【发布时间】:2017-07-11 13:26:25
【问题描述】:
我正在尝试在我的项目中使用 uber 进行身份验证,转到 uber 原生应用程序然后返回我的应用程序的路径是可以的。但是,它只返回 TokenString 和 ExpirationDate,而 refreshToken 返回为 nil。
这是我的代码
AuthorizationBaseViewController
class AuthorizationBaseViewController: UIViewController {
func delay(delay: Double, closure: ()->()) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay*Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
}
func showMessage(message: String) {
let alert = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let okayAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
alert.addAction(okayAction)
self.presentViewController(alert, animated: true, completion: nil)
}
func checkError(response: Response) {
// Unauthorized
if response.statusCode == 401 {
TokenManager.deleteToken()
dispatch_async(dispatch_get_main_queue(), {
self.reset()
})
}
}
func reset() {
}
// Mark: LoginButtonDelegate
func loginButton(button: LoginButton, didLogoutWithSuccess success: Bool) {
if success {
showMessage(NSLocalizedString("Integration with uber canceled.", comment: ""))
}
}
func loginButton(button: LoginButton, didCompleteLoginWithToken accessToken: AccessToken?, error: NSError?) {
if let _ = accessToken {
print(accessToken?.tokenString)
print(accessToken?.expirationDate)
print(accessToken?.refreshToken)
showMessage(NSLocalizedString("Uber user authenticated successfully.", comment: ""))
if let url = NSURL(string: "xxxxxxxxxxxxxx") {
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
let token = AccessToken?()
let jsonObject = ["token" : (token?.tokenString)!, "refresh_token" : (token?.refreshToken)!,"expires_in" : (token?.expirationDate)!, "user_id" : "uber_uuid" , "token_type" : "Bearer"] as Dictionary <String,AnyObject>
request.HTTPBody = try? NSJSONSerialization.dataWithJSONObject(jsonObject, options: [])
NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard
let data = data where
error == nil &&
(response as? NSHTTPURLResponse)?.statusCode == 200
else {
print((response as? NSHTTPURLResponse)?.statusCode ?? "no status code")
print(error?.localizedDescription ?? "no error description")
return
}
print(String(data: data, encoding: NSUTF8StringEncoding) ?? "no string from data")
}.resume()
}
showMessage((error?.localizedDescription)!)
} else if let error = error {
showMessage(error.localizedDescription)
} else {
showMessage("Error")
}
}
}
登录视图控制器
class ImplicitGrantLoginViewController: AuthorizationBaseViewController, LoginButtonDelegate {
/// The LoginManager to use for login
let loginManager = LoginManager(loginType: .Native)
/// The RidesClient to use for endpoints
let ridesClient = RidesClient()
// The Uber button to use for UI
var uberLoginButton: LoginButton?
// The Uber Scopes
var uberScopes: [RidesScope]?
@IBOutlet weak var logoutBgView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
//UberScopes to get authentication
uberScopes = [.History, .Profile, .HistoryLite,.Places, .RideWidgets]
uberLoginButton = LoginButton(frame: CGRectZero,scopes:uberScopes! ,loginManager: loginManager)
// Uber Login Button Creation
let loginButton = LoginButton(frame: CGRectZero, scopes: uberScopes!, loginManager: loginManager)
loginButton.presentingViewController = self
loginButton.delegate = self
loginButton.frame = logoutBgView.bounds
loginButton.autoresizingMask =
[.FlexibleWidth, .FlexibleHeight]
logoutBgView.addSubview(loginButton)
}
基本上,如果刷新令牌返回到我的应用程序,则会发出 POST 请求以及 TokenString 和 ExpirationDate
身份验证完成后,在显示授权视图之前会出现以下错误
MyApp[18136:615342] -canOpenURL:URL 失败:“uberauth://connect?third_party_app_name=MyApp &callback_uri_string=xxxxxx &client_id=xxxxxxxxxx &login_type=default&scope=history%20profile%20history_lite%20places%20ride_widgets&sdk=ios&sdk_version=0.6。 0
即使出现此错误,也会显示授权范围的屏幕,当我点击允许时,我会在调试区域看到返回,但是由于 refreshtoken 为 nil 并且 HTTP 请求未接收到它,因此应用程序崩溃。
由于 refreshtoken 为 nil 导致 App 崩溃时出错
我已经检查了 plist 文件并根据 uber 文档/github 存储库填写。 Callback URI, LSApplicationQuerieScheme,Client ID,DisplayName 正确。
【问题讨论】:
-
根据错误消息,我假设您错过了 README 中提出的安全更改,以便启用对
uberauth的调用。 -
@agraebe 我已经在 LSApplicationQuerieSchemes 中设置了这个
-
为什么要关心刷新令牌?它被标记为可选,所以你必须做好准备让它不在那里。