我使用this库尤伯杯Authetication尤伯杯验证失败 “HTTP状态401:未经授权,响应:{” 错误 “: ”invalid_client“}”
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:未经授权,响应:{ “错误”: “invalid_client”}
我有三重检查,我的client_id(consumerKey)和秘密(consumerSecret)是正确的。 我已经在这里做了错事
更新:1
这是有线我改变的responseType:“代码”来的responseType:“令牌”和它的工作得到了我的访问令牌。但我得到的另一个问题现在
现在当过我尝试使用下面的代码来调用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"}
}
}
尤伯杯API需要的OAuth2但您使用的LIB的您好!OAuth1的格式。检查https://github.com/dongri/OAuthSwift#examples。并且查看[Uber API文档](https://developer.uber.com/v1/auth/),auth以*三个步骤完成,而不是一个。 :) – Moritz
@EricD。感谢您的回应。如何使用这个库来使用Oauth2。让我知道这有什么问题。你可以看到我已经使用OAuth2Swift –
你正在使用正确的'OAuth2Swift'对象,*但*在其中使用了错误的声明格式。查看OAuthSwift页面上的* last *示例,其中不应该有任何'accessTokenUrl'。一旦你得到了这个回应,请遵循*重定向*,然后最终得到令牌。 :) – Moritz