ios - OAuth2、 swift 3、Instagram

标签 ios swift swift3 instagram oauth-2.0

IG 似乎有很多变化。许多OAuth2 repos,似乎都有错误,或者真的不容易转换为Swift3。想知道是否有人有迁移到 Swift3 并使用 Instagram 最新更改的解决方案?

最受欢迎的解决方案。 OAuth2 的实现似乎是其中一个更复杂的事情。令人惊讶的是 IG 没有提供他们自己的示例文档来说明如何使用 iOS 执行此操作。他们只有基于网络的解决方案的文档。

也许那里正在酝酿什么?他们有数以百万计的编码员。但就目前而言,正在寻找(我敢说?)简单的解决方案。

感谢一百万。 :-)

最佳答案

对于 Swift 3:

更新:2017 年 4 月 17 日:由于依赖关系损坏,Pods 安装不再有效。因此,我撕下了所需的内容并创建了一个 new Github project 使用桥接头并将所有需要的文件存储在项目中。如果您克隆或下载 github 项目,您将能够立即登录 Instagram。

要在您的项目中使用这些文件,只需将所有文件从 SimpleAuth 文件夹拖放到您的项目中,确保标记copy item if needed

enter image description here

您还需要在 Instagram 开发者控制台中禁用 Disable implicit oAuth

enter image description here

然后您可以将我的代码从桥接 header 复制/粘贴到您的代码中,或者使用我的代码。在目标的build设置中设置桥接头。

enter image description here

其他一切都和以前一样:

我有一个 Instagram 帐户的结构:

struct InstagramUser {

    var token: String = ""
    var uid: String = ""
    var bio: String = ""
    var followed_by: String = ""
    var follows: String = ""
    var media: String = ""
    var username: String = ""
    var image: String = ""
}

接收token的函数:

typealias JSONDictionary = [String:Any]
var user: InstagramUser?
let INSTAGRAM_CLIENT_ID = "16ee14XXXXXXXXXXXXXXXXXXXXXXXXX"
let INSTAGRAM_REDIRECT_URI = "http://www.davidseek.com/just_a_made_up_dummy_url" //just important, that it matches your developer account uri at Instagram

extension ViewController {

    func connectInstagram() {

        let auth: NSMutableDictionary = ["client_id": INSTAGRAM_CLIENT_ID,
                                         SimpleAuthRedirectURIKey: INSTAGRAM_REDIRECT_URI]

        SimpleAuth.configuration()["instagram"] = auth            
        SimpleAuth.authorize("instagram", options: [:]) { (result: Any?, error: Error?) -> Void in

            if let result = result as? JSONDictionary  {

                var token = ""
                var uid = ""
                var bio = "" 
                var followed_by = ""
                var follows = ""
                var media = ""
                var username = ""
                var image = ""

                token = (result["credentials"] as! JSONDictionary)["token"] as! String
                uid = result["uid"] as! String

                if let extra = result["extra"] as? JSONDictionary,
                    let rawInfo = extra ["raw_info"] as? JSONDictionary,
                    let data = rawInfo["data"] as? JSONDictionary {

                    bio = data["bio"] as! String

                    if let counts = data["counts"] as? JSONDictionary {
                        followed_by = String(describing: counts["followed_by"]!)
                        follows = String(describing: counts["follows"]!)
                        media = String(describing: counts["media"]!)
                    }
                }

                if let userInfo = result["user_info"] as? JSONDictionary {
                    username = userInfo["username"] as! String
                    image = userInfo["image"] as! String
                }

                self.user = InstagramUser(token: token, uid: uid, bio: bio, followed_by: followed_by, follows: follows, media: media, username: username, image: image)


            } else {
                // this handles if user aborts or the API has a problem like server issue
                let alert = UIAlertController(title: "Error!", message: nil, preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
                self.present(alert, animated: true, completion: nil)
            }

            if error != nil {
                print("Error during SimpleAuth.authorize: \(error)")
            }
        }
    }
}

Instagram 还说:

Important

Even though our access tokens do not specify an expiration time, your app should handle the case that either the user revokes access, or Instagram expires the token after some period of time. If the token is no longer valid, API responses will contain an “error_type=OAuthAccessTokenException”. In this case you will need to re-authenticate the user to obtain a new valid token. In other words: do not assume your access_token is valid forever.

所以处理接收到OAuthAccessTokenException

的情况

关于ios - OAuth2、 swift 3、Instagram,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40286744/

相关文章:

iphone - 将 Flurry AppCircle 广告与 AdMob 中介集成

android - 在所有 3 个平台(android、ios、windows 手机)上推送通知

ios - 合并具有相同父类(super class)的 Swift 数组

ios - 自定义单元格标签仅在选择行时显示

arrays - 数组或字典扩展中的 Swift 3.0 : compiler error when calling global func min<T>(T, T)

ios - 组合不同语言 RTL 和 LTR 中的两个字符串

ios - 在App Store中更改应用程序名称会更改URL,那么ASO呢?

ios - 如何在 Swift 3 中将核心数据导出为 CSV?

ios - Alamofire 4.0 的证书无效问题

ios - CNContactPickerViewController导航栏颜色