在 iOS 上使用 OAuth2 进行身份验证
Authenticating with OAuth2 on iOS
我目前正在尝试使用 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()
当我 运行 它在浏览器中加载 google 并且我能够登录。然后它询问我在范围内声明的权限并且工作正常。我单击“确定打开”,它会将我重定向回我的应用程序。
然而,当我再次 运行 这段代码时,我希望访问令牌已存储在钥匙链中。然而,这似乎不起作用。
我查看了源代码并发现了以下检查:tryToObtainAccessTokenIfNeeded
始终 returns 错误。这意味着我再次获得需要单击 'Allow'.
的页面
我想知道是否有人可以帮我弄清楚为什么它没有在钥匙串中保存任何东西。这是否也意味着用户没有真正通过身份验证?
谢谢。
===
编辑
已根据 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",
"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()
}
// 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
}
希望这会帮助其他可能在尝试获取访问令牌时遇到问题的人。
我目前正在尝试使用 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()
当我 运行 它在浏览器中加载 google 并且我能够登录。然后它询问我在范围内声明的权限并且工作正常。我单击“确定打开”,它会将我重定向回我的应用程序。
然而,当我再次 运行 这段代码时,我希望访问令牌已存储在钥匙链中。然而,这似乎不起作用。
我查看了源代码并发现了以下检查:tryToObtainAccessTokenIfNeeded
始终 returns 错误。这意味着我再次获得需要单击 'Allow'.
我想知道是否有人可以帮我弄清楚为什么它没有在钥匙串中保存任何东西。这是否也意味着用户没有真正通过身份验证?
谢谢。
===
编辑
已根据 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",
"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()
}
// 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
}
希望这会帮助其他可能在尝试获取访问令牌时遇到问题的人。