我目前正在尝试使用 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。这意味着我再次进入需要单击“允许”的页面。
我想知道是否有人可以帮助我弄清楚为什么它没有在钥匙串中保存任何内容。这是否也意味着用户没有真正经过身份验证?
Thanks.
===
Edit
已添加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
这就是我所认为正在发生的事情。但是我仍然不确定为什么它没有保存/在钥匙串中找到任何内容。
=====
Edit 2
事实证明,我实际上根本没有拿回访问令牌。请看这段对话:https://github.com/p2/OAuth2/issues/109以及我的回答如下。