OAuth2,Swift 3,Instagram [英] OAuth2, Swift 3, Instagram

查看:109
本文介绍了OAuth2,Swift 3,Instagram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IG似乎有很多变化.许多OAuth2存储库似乎都存在错误,或者实际上不容易转换为Swift3.想知道是否有人有解决方案来迁移到Swift3并使用Instagram的最新更改?

There seem to be lots of changes at IG. Many OAuth2 repos, all seem to have bugs, or really not easily converted to Swift3. Wondering if anyone has a solution for moving to Swift3 and working with the latest changes at Instagram?

最欢迎的解决方案. OAuth2实现似乎是其中更复杂的事情之一.令我惊讶的是,IG没有提供自己的示例文档,说明如何使用iOS进行此操作.他们只有针对基于Web的解决方案的文档.

Solutions most welcome. OAuth2 implementation seems to one of the more complicated things out there. Surprised that IG has not offered their own example docs on how to do this with iOS. They only have docs for web based solutions.

也许有些东西正在酝酿中?他们拥有数以万计的编码人员.但是目前,正在寻求一种(敢说吗?)简单的解决方案.

Maybe something brewing there? Zillions of coders they have on staff. But for now, on the hunt for a (dare I say?) simple solution.

感谢一百万. :-)

推荐答案

对于Swift 3:

更新:2017年4月17日:由于依赖关系断开,Pods的安装不再起作用.因此,我已经摘录了所需的内容,并使用桥接创建了 新的Github项目 标头并将所有需要的文件存储在项目中.如果您克隆或下载github项目,则可以立即登录Instagram.

Update: April 17 2017: Because of broken dependencies, the installation by Pods is no longer working. Therefore I have ripped the needed content and created a new Github project using a Bridging Header and stored all the needed files within the project. If you clone or download the github project, you'll instantly be able to login to Instagram.

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

To use those files in your project, simply drag and drop all the files from the SimpleAuth folder to your project, make sure to mark copy item if needed

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

Also you need to disable Disable implicit oAuth within the Instagram developer console.

然后,您将我的代码从桥接头复制/粘贴到您的文件中,或者使用我的代码.在目标的构建设置中设置桥接头.

Then you either copy/paste my code from the Bridging Header into your or you use mine. Set the Bridging Header at the Target's Build Settings.

其他所有内容都像以前一样

Everything else works as before:

我有一个Instagram帐户结构:

I have a struct for the Instagram Account:

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 = ""
}

接收令牌的功能:

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还说:

Instagram also says:

重要

即使我们的访问令牌未指定到期时间,您的 应用应处理以下情况:用户撤销访问权限,或 Instagram会在一段时间后使令牌过期.如果令牌是 不再有效,API响应将包含一个 "error_type = OAuthAccessTokenException".在这种情况下,您将需要 重新验证用户以获得新的有效令牌.换一种说法: 不要假设您的access_token永远有效.

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

这篇关于OAuth2,Swift 3,Instagram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆