在iOS上使用OAuth2进行身份验证

在iOS上使用OAuth2进行身份验证,第1张

概述我目前正在尝试使用OAuth2授权我的用户.我目前正在使用以下库: https://github.com/p2/OAuth2 let oauth2 = OAuth2CodeGrant(settings: [ "client_id": "my-id", "authorize_uri": "https://accounts.google.com/o/oauth2/aut 我目前正在尝试使用OAuth2授权我的用户.我目前正在使用以下库: https://github.com/p2/OAuth2

let oauth2 = OAuth2CodeGrant(settings: [        "clIEnt_ID": "my-ID","authorize_uri": "https://accounts.Google.com/o/oauth2/auth","token_uri": "https://www.GoogleAPIs.com/oauth2/v3/token","scope": "profile",// depends on the API you use        "redirect_uris": ["com.TestAuthorizeApp:/oauth2Callback"],])    //let oauth2 = OAuth2CodeGrant(settings: settings)    oauth2.onAuthorize = { parameters in        print("DID authorize with parameters: \(parameters)")    }    oauth2.onFailure = { error in        // `error` is nil on cancel        if let error = error {            print("Authorization went wrong: \(error)")        }    }    oauth2.authConfig.authorizeEmbedded = false    oauth2.authorize()

当我运行它时,它在浏览器中加载谷歌,我能够登录.然后它询问我在范围内声明的权限,并且工作正常.我点击确定打开,它将我重定向回我的应用程序.

但是,当我再次运行此代码时,我希望访问令牌已存储在密钥链中.然而,这似乎不起作用.

我查看了源代码并找到了以下检查:tryToObtainAccesstokenIfNeeded,它总是返回false.这意味着我再次获取页面,我需要单击“允许”.

我想知道是否有人可以帮我弄清楚为什么它不能保存钥匙串中的任何东西.这是否意味着用户并未真正进行身份验证?

谢谢.

===

编辑

根据Pascal的评论添加了oauth2.verbose = true.我得到以下输出.

OAuth2: Looking for items in keychain OAuth2: No access token,maybe I can refresh OAuth2: I don't have a refresh token,not trying to refresh

这就是我的想法.但是我仍然不确定它为什么不在钥匙串中保存/找到任何东西.

=====

编辑2

事实证明,我实际上根本没有获得访问令牌.请看这个对话:https://github.com/p2/OAuth2/issues/109和我的答案如下.

解决方法 在Pascal的帮助下: https://github.com/p2/OAuth2/issues/109我设法让它工作.事实证明我没有实施步骤:’3授权用户’,因为我应该这样做.

所以完整的解决方案是:

在我的视图控制器中,我有以下内容:

let OAuth2AppDIDReceiveCallbackNotification = "OAuth2AppDIDReceiveCallback"overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    // This notification is for handling step 3 in guIDe.    NSNotificationCenter.defaultCenter().addobserver(self,selector: #selector(VIEwController.handleRedirect(_:)),name: OAuth2AppDIDReceiveCallbackNotification,object: nil)}func authoriseUser {    let oauth2 = OAuth2CodeGrant(settings: [        "clIEnt_ID": "my-ID",// Use own clIEnt_ID here        "authorize_uri": "https://accounts.Google.com/o/oauth2/auth",])     //let oauth2 = OAuth2CodeGrant(settings: settings)     oauth2.onAuthorize = { parameters in        print("DID authorize with parameters: \(parameters)")     }     oauth2.onFailure = { error in        // `error` is nil on cancel         if let error = error {             print("Authorization went wrong: \(error)")         }     }     oauth2.authConfig.authorizeEmbedded = false     oauth2.authorize() }// This method gets called by notification and is the last thing we need to do to get our access token. func handleRedirect(notification: NSNotification) {    oauth2.handleRedirectURL(notification.object as! NSURL)}

上面的代码应该处理将您发送到您可以登录的Google网页,然后点击允许.

现在您需要处理在应用程序委托中返回应用程序:

let OAuth2AppDIDReceiveCallbackNotification = "OAuth2AppDIDReceiveCallback" func application(application: UIApplication,openURL url: NSURL,sourceApplication: String?,annotation: AnyObject) -> Bool {    // you should probably first check if this is your URL being opened    NSNotificationCenter.defaultCenter().postNotificationname(OAuth2AppDIDReceiveCallbackNotification,object: url)    return true}

希望这将有助于其他可能在尝试获取访问令牌时遇到问题的人.

总结

以上是内存溢出为你收集整理的在iOS上使用OAuth2进行身份验证全部内容,希望文章能够帮你解决在iOS上使用OAuth2进行身份验证所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1062293.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存