Swift OAuth2.0 与 redirectURI
Swift OAuth2.0 with redirectURI
我正在使用提供 OAuth2.0 身份验证的服务。这是我需要的步骤:
- 打开一个 URL 以用户 ID 作为参数
- 用户批准我的应用程序(已正确注册)。
- 用户被重定向到 RedirectUri,哈希中有访问令牌。
第三点是我的主要问题
我已经使用微软库实现了 OAuth,一切正常。但我不能在这里使用它们,所以我正在尝试 https://github.com/OAuthSwift/OAuthSwift 这个。
这是我的代码:
private func authenticationService() {
// create an instance and retain it
let oauthswift = OAuth2Swift(
consumerKey: "xx",
consumerSecret: "xxx",
authorizeUrl: "//myurl + userId",
responseType: "token"
)
oauthswift.authorizeURLHandler = OAuthSwiftOpenURLExternally.sharedInstance
let handle = oauthswift.authorize(
withCallbackURL: "???",
scope: "", state:"") { result in
switch result {
case .success(let (credential, response, parameters)):
print(credential.oauthToken)
// Do your request
case .failure(let error):
print(error.localizedDescription)
}
}
}
这可以正确打开我的 Safari,但随后我被重定向到哈希中包含访问令牌的 URI,但没有任何反应。
这里的主要问题是我有一个重定向 uri,所以我猜回调 URL 没有被调用?这不是打开 sheet,而是重定向到 Safari。而且我不喜欢这种方法。
如何通过上述步骤在 swift 中执行 OAuth2.0?如何从 url 获取访问令牌?什么是最好的图书馆,我怎样才能充分利用它?
更新:
这是我的 stackExchange 代码:
let request: OAuth2Request = .init(authUrl: "https://stackexchange.com/oauth/dialog?client_id=<MYCLIENTID>&scope=private_info&redirect_uri=https://stackexchange.com/oauth/login_success",
tokenUrl: "https://whosebug.com/oauth/access_token/json",
clientId: "<MYCLIENTID>",
redirectUri: "https://stackexchange.com/oauth/login_success",
clientSecret: "",
scopes: [])
堆栈应用程序中的 OAuth 域是 => stackexchange.com
所以我在 URL 中添加了以下内容: redirect-uri:// (即使没有 <>)
但每次我批准我的应用程序时,我都被堆叠在包含我的令牌的“授权应用程序”中,并且我没有被重定向。
在下面的代码中:
private func authenticationService() {
// create an instance and retain it
let oauthswift = OAuth2Swift(...)
你不保留 oauthswift
(也不handle
)。一旦您执行完 authenticationService
函数,它们就会被释放。
您需要在函数外部存储对 oauthswift
和 handle
的引用(在 class 级别)。
let oauthswift: OAuth2Swift
let handle: ...
init() {
oauthswift = OAuth2Swift(
consumerKey: "xx",
consumerSecret: "xxx",
authorizeUrl: "//myurl + userId",
responseType: "token"
)
...
}
private func authenticationService() {
handle = oauthswift.authorize(...)
}
如果您的目标是 iOS13,您可以使用 Apple 提供的新 AuthenticationServices 库。
它适用于 macOS 和 iOS。
也许这会对其他开发人员有所帮助,我创建了一个简单而小巧的 swift 包来处理 Swift 中的 OAuth2,您可以查看演示项目,它运行良好
https://github.com/hadiidbouk/SimpleOAuth2
编辑:
您传递的 URL 有误,它们应该是这样的
let request: OAuth2Request = .init(authUrl: "https://whosebug.com/oauth",
tokenUrl: "https://whosebug.com/oauth/access_token/json",
clientId: "<<your client id>>",
redirectUri: "redirect-uri://stackexchange.com",
clientSecret: "<<your client secret>>",
scopes: ["private_info"])
我正在使用提供 OAuth2.0 身份验证的服务。这是我需要的步骤:
- 打开一个 URL 以用户 ID 作为参数
- 用户批准我的应用程序(已正确注册)。
- 用户被重定向到 RedirectUri,哈希中有访问令牌。
第三点是我的主要问题
我已经使用微软库实现了 OAuth,一切正常。但我不能在这里使用它们,所以我正在尝试 https://github.com/OAuthSwift/OAuthSwift 这个。
这是我的代码:
private func authenticationService() {
// create an instance and retain it
let oauthswift = OAuth2Swift(
consumerKey: "xx",
consumerSecret: "xxx",
authorizeUrl: "//myurl + userId",
responseType: "token"
)
oauthswift.authorizeURLHandler = OAuthSwiftOpenURLExternally.sharedInstance
let handle = oauthswift.authorize(
withCallbackURL: "???",
scope: "", state:"") { result in
switch result {
case .success(let (credential, response, parameters)):
print(credential.oauthToken)
// Do your request
case .failure(let error):
print(error.localizedDescription)
}
}
}
这可以正确打开我的 Safari,但随后我被重定向到哈希中包含访问令牌的 URI,但没有任何反应。
这里的主要问题是我有一个重定向 uri,所以我猜回调 URL 没有被调用?这不是打开 sheet,而是重定向到 Safari。而且我不喜欢这种方法。
如何通过上述步骤在 swift 中执行 OAuth2.0?如何从 url 获取访问令牌?什么是最好的图书馆,我怎样才能充分利用它?
更新:
这是我的 stackExchange 代码:
let request: OAuth2Request = .init(authUrl: "https://stackexchange.com/oauth/dialog?client_id=<MYCLIENTID>&scope=private_info&redirect_uri=https://stackexchange.com/oauth/login_success",
tokenUrl: "https://whosebug.com/oauth/access_token/json",
clientId: "<MYCLIENTID>",
redirectUri: "https://stackexchange.com/oauth/login_success",
clientSecret: "",
scopes: [])
堆栈应用程序中的 OAuth 域是 => stackexchange.com
所以我在 URL 中添加了以下内容: redirect-uri://
但每次我批准我的应用程序时,我都被堆叠在包含我的令牌的“授权应用程序”中,并且我没有被重定向。
在下面的代码中:
private func authenticationService() {
// create an instance and retain it
let oauthswift = OAuth2Swift(...)
你不保留 oauthswift
(也不handle
)。一旦您执行完 authenticationService
函数,它们就会被释放。
您需要在函数外部存储对 oauthswift
和 handle
的引用(在 class 级别)。
let oauthswift: OAuth2Swift
let handle: ...
init() {
oauthswift = OAuth2Swift(
consumerKey: "xx",
consumerSecret: "xxx",
authorizeUrl: "//myurl + userId",
responseType: "token"
)
...
}
private func authenticationService() {
handle = oauthswift.authorize(...)
}
如果您的目标是 iOS13,您可以使用 Apple 提供的新 AuthenticationServices 库。
它适用于 macOS 和 iOS。
也许这会对其他开发人员有所帮助,我创建了一个简单而小巧的 swift 包来处理 Swift 中的 OAuth2,您可以查看演示项目,它运行良好
https://github.com/hadiidbouk/SimpleOAuth2
编辑:
您传递的 URL 有误,它们应该是这样的
let request: OAuth2Request = .init(authUrl: "https://whosebug.com/oauth",
tokenUrl: "https://whosebug.com/oauth/access_token/json",
clientId: "<<your client id>>",
redirectUri: "redirect-uri://stackexchange.com",
clientSecret: "<<your client secret>>",
scopes: ["private_info"])