【发布时间】:2015-07-16 08:56:08
【问题描述】:
我正在使用 this 库进行 Uber 身份验证
https://developer.uber.com/v1/auth/
我已经这样做了
func doOAuthUber(){
let oauthswift = OAuth2Swift(
consumerKey: "fXfXXXXXXXUo9vtKzobXXXXXUDO",
consumerSecret: "e5XXXXXXXq2w63qz9szEx7uXXXXXXo03W",
authorizeUrl: "https://login.uber.com/oauth/authorize",
accessTokenUrl: "https://login.uber.com/oauth/token",
responseType: "code"
)
var originalString = "jamesappv2://oauth/callback"
var encodedCallBackUrl = originalString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
println("encodedCallBackUrl: \(encodedCallBackUrl)")
let state: String = ""
oauthswift.authorizeWithCallbackURL( NSURL(string: encodedCallBackUrl!)!, scope: "request%20history", state: state, success: {
credential, response in
println(credential.oauth_token)
self.personalDriverLoader.stopAnimating()
}, failure: {(error:NSError!) -> Void in
self.personalDriverLoader.stopAnimating()
println(error.localizedDescription)
})
}
但收到此回复 HTTP 状态 401:未经授权,响应:{"error": "invalid_client"}
我已经三次检查我的 client_id (consumerKey) 和 secret (consumerSecret) 是否正确。 我在这里做错了什么
更新:1
这是有线的,我将 responseType: "code" 更改为 responseType: "token" 并且它工作得到了我的访问令牌。但我现在遇到了另一个问题
现在每当我尝试拨打request endpoint api
使用下面的代码
@IBAction func btnRequestUberdidClicked(sender: AnyObject) {
self.callRequestAPI("https://sandbox-api.uber.com/v1/requests")
}
func callRequestAPI(url:String){
var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
var session = NSURLSession(configuration: configuration)
let params:[String: AnyObject] = [
"product_id" : selectedUberProductId,
"start_latitude" : start_lat,
"start_longitude" : start_lng,
"end_latitude" : end_lat,
"end_longitude" : end_lng]
appDelegate.oauthswift.client.post(url, parameters: params,
success: { data, response in
let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Success")
println(data)
println(response)
}, failure: {(error:NSError!) -> Void in
println("Error")
println(error)
})
}
我收到此回复
Error Domain=NSURLErrorDomain Code=401 "HTTP Status 401: Unauthorized, Response: {"message":"Invalid OAuth 2.0 credentials provided.","code":"unauthorized"}" UserInfo=0x1c563220 {NSLocalizedDescription=HTTP Status 401: Unauthorized, Response: {"message":"Invalid OAuth 2.0 credentials provided.","code":"unauthorized"}, Response-Headers=<CFBasicHash 0x1c578c40 [0x35305710]>{type = immutable dict, count = 7,
entries =>
1 : x-xss-protection = <CFString 0x1ae2fc60 [0x35305710]>{contents = "1; mode=block"}
4 : Server = <CFString 0x1acc24c0 [0x35305710]>{contents = "nginx"}
5 : Content-Type = <CFString 0x1c4d0020 [0x35305710]>{contents = "application/json"}
6 : Content-Length = <CFString 0x1c4b70b0 [0x35305710]>{contents = "75"}
8 : Date = <CFString 0x1c4ed4b0 [0x35305710]>{contents = "Wed, 06 May 2015 12:46:51 GMT"}
10 : Strict-Transport-Security = <CFString 0x1c225cb0 [0x35305710]>{contents = "max-age=31536000; includeSubDomains; preload"}
11 : x-uber-app = <CFString 0x1c49a6b0 [0x35305710]>{contents = "uberex-sandbox"}
}
}
【问题讨论】:
-
Uber API 需要 OAuth2,但您使用的是 OAuth1 格式的库。检查github.com/dongri/OAuthSwift#examples。在Uber API doc 中查看,身份验证是在三个 步骤中完成的,而不是一个。 :)
-
@EricD。感谢您的回复。如何使用这个库来使用 Oauth2。让我知道这有什么问题。你可以看到我使用过 OAuth2Swift
-
您使用了正确的
OAuth2Swift对象,但在其中您使用了错误的声明格式。查看 OAuthSwift 页面上的 last 示例,其中不应该有任何accessTokenUrl。得到此响应后,请按照 redirect 进行操作,然后最终获得令牌。 :) -
这是有线的,我将 responseType: "code" 更改为 responseType: "token" 并且它起作用了 (o0) 。得到了我的访问令牌。但我现在遇到了另一个问题
-
@EricD。查看我的更新
标签: ios iphone swift authentication uber-api