我正在尝试使用ASWebAuthenticationSession从Stripe's OAuth endpoint获取验证码-此事件在我的条纹重定向url被调用后发生。
不幸的是,authSession的完成处理程序不会使用callbackURL回调。我需要这个callbackURL来查询授权码。我读过关于这个主题的不同文章,但我不明白为什么我的实现不能像我期望的那样工作。
下面是我的代码:
class CreateStripeConnectAccountController: UIViewController {
var authSession: ASWebAuthenticationSession!
override func viewDidLoad() {
super.viewDidLoad()
configureAuthSession()
}
private func configureAuthSession() {
let urlString = Constants.URLs.stripeConnectOAuth // Sample URL
guard let url = URL(string: urlString) else { return }
let callbackScheme = "myapp:auth"
authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackScheme, completionHandler: { (callbackURL, error) in
guard error == nil, let successURL = callbackURL else {
print("Nothing")
return
}
let oauthToken = NSURLComponents(string: (successURL.absoluteString))?.queryItems?.filter({$0.name == "code"}).first
print(successURL.absoluteString)
})
authSession.presentationContextProvider = self
authSession.start()
}
}
extension CreateStripeConnectAccountController: ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
self.view.window ?? ASPresentationAnchor()
}
}发布于 2020-02-26 02:35:38
我认为问题是你用callbackURLScheme换来了nil。您需要提供一个重定向到您的应用程序的URL方案:
查看苹果的身份验证示例:https://developer.apple.com/documentation/authenticationservices/authenticating_a_user_through_a_web_service
下面是关于如何为你的应用创建自定义URL方案的苹果文档:https://developer.apple.com/documentation/uikit/inter-process_communication/allowing_apps_and_websites_to_link_to_your_content/defining_a_custom_url_scheme_for_your_app。
发布于 2020-04-23 18:46:25
我知道这很老,但不管怎样。确保redirect_uri中使用的callbackScheme和方案是相同的。
发布于 2021-06-18 23:04:58
您的callbackScheme myapp:auth格式不正确。
不能在URI的方案名称中使用符号:。
请参阅以下Scheme的RFC定义。
方案名称由一系列字符组成,以字母开头,后跟字母、数字、加号("+")、句点(".")或连字符("-")的任意组合。
https://datatracker.ietf.org/doc/html/rfc3986#section-3.1
因此,将callbackURLscheme修改为myapp-auth或myapp.auth效果很好。
https://stackoverflow.com/questions/60398648
复制相似问题