我正在使用新的Lyft API,它在認證部分中說它發送訪問令牌請求並顯示示例響應, 「ACCESS_TOKEN」。訪問來自NSURLSession(Lyft API)的JSON響應中的鍵值的問題
樣本響應
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
"access_token": <access_token>,
"token_type":"bearer",
"expires_in":3600,
"scope": "public"
}
然而,當我撥打電話(是的客戶端ID和祕密是正確的,但我不能張貼在這裏),它返回一個響應,但訪問令牌是零。
感謝您一如既往的幫助!文件鏈接下面,
API文檔,
https://developer.lyft.com/docs/authentication
func authorize() {
let authString = NSString(format: "%@:@%", clientID, clientSecret)
let authData = authString.dataUsingEncoding(NSUTF8StringEncoding)
let base64String = authData!.base64EncodedDataWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
let url = NSURL(string: "https://api.lyft.com/oauth/token")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
request.setValue("Basic \(base64String)", forHTTPHeaderField: "Authorization")
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
let dictionary = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
if let accessToken : String = dictionary["access_token"] as? String {
print(accessToken)
}
}
task.resume()
}
如果你打電話'base64EncodedStringWithOptions',而不是'base64EncodedDataWithOptions'會發生什麼? – bringel
確實發生了同樣的事情......雖然我從與Lyft的支持人員交談中發現它應該是一個「POST」而不是「GET」,雖然即使那樣我也沒有得到正確的答案。 – Echizzle
@Echizzle:我正在做同樣的整合,你有什麼更新。 –