问题描述
我正在尝试使用ASWebAuthentication来简化Oauth 1.0的身份验证过程。之后 用户输入其凭据并批准我的申请,oauth提供商使用我传入的com.me.appName:/ returnToApp的redirect_url Safari窗口如下所示:
这是我的代码:
func getAuthTokenWithWebLogin(context: ASWebAuthenticationPresentationContextProviding) {
let callbackUrlScheme = “scheme:/returnToApp"
let authURL = URL(string: Constants.authURL)
guard authURL != nil else{return}
let webAuthSession = aswebauthenticationsession.init(url: authURL!,callbackURLScheme: callbackUrlScheme,completionHandler: { (callBack:URL?,error:Error?) in
// handle auth response
guard error == nil,let successURL = callBack else {
return
}
let oauthToken = NSURLComponents(string: (successURL.absoluteString))?.queryItems?.filter({$0.name == "code"}).first
// Do what you Now that you've got the token,or use the callBack URL
print(oauthToken ?? "No OAuth Token")
})
// New in iOS 13
webAuthSession.presentationContextProvider = context
// Kick it off
webAuthSession.start()
}
我认为这与ASWebAuthentication无关,因为在尝试使用第三方库OauthSwift时遇到了同样的问题
解决方法
我的坏;我没有意识到callbackURL必须采用myApp://格式,而我将自己的名称设为myApp:/(单斜杠)认为这也可以。