1
我嘗試創建一個與Uber API集成的應用程序。刷新超級令牌Swift
我可以得到我的oauth標記,但是當我嘗試刷新標記但API返回401錯誤。 我做一個POST請求到這個端點 https://login.uber.com/oauth/v2/token 有了這個PARAMS:
let params = ["client_secret": ***********, "client_id": ***********, "grant_type": "refresh_token", "redirect_uri": "REDIRECT_URI", "refresh_token": refresh_token]
refresh_token是當我連接用戶,其包含尤伯杯API的refresh_token鍵返回一個變種。
任何人有任何想法如何處理刷新我的令牌?
感謝
編輯:
ViewController.swift
UberManager.refreshToken { (changed) -> Void in
if let change = changed {
print("token renewable")
}
}
UberManager.swift
public class UberManager {
static func refreshToken(completionHandler:(changed: Bool?) -> Void) -> Void {
Router.RefreshToken().performRequest { (json, error) -> Void in
if error == nil {
print(json)
}
}
}
}
Router.swift
enum Router: URLRequestConvertible {
static let baseURL = Constants.APIConstants.api_url
static let sandboxURL = Constants.APIConstants.sandbox_api
static let tokenURL = Constants.APIConstants.login_url
case RefreshToken()
var method: Alamofire.Method {
switch self {
case .RefreshToken():
return .POST
}
}
var path: String {
switch self {
case .RefreshToken():
return "/v2/token"
}
}
var URLRequest: NSMutableURLRequest {
var URL = NSURL()
switch self {
case .RefreshToken():
URL = NSURL(string: Router.tokenURL)!
default:
// TODO : change this when App is ready
URL = NSURL(string: Router.sandboxURL)!
}
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))
mutableURLRequest.HTTPMethod = method.rawValue
if let token = Session.sessionInfos().access_token {
mutableURLRequest.setValue("Bearer \(token)", forHTTPHeaderField: "authorization")
}
switch self {
case .RefreshToken():
var params = [String: String]()
if let refresh_token = Session.sessionInfos().refresh_token {
params = ["client_secret": Constants.APIConstants.kSecret,
"client_id": Constants.APIConstants.kIDClient,
"grant_type": "refresh_token",
"redirect_uri": Constants.APIConstants.redirect_uri,
"refresh_token": refresh_token]
}
return ParameterEncoding.URL.encode(mutableURLRequest, parameters: params).0
}
}
func performRequest(completionHandler: (AnyObject?, AppError?) -> Void) {
request(self).responseData({ (response) -> Void in
switch response.result {
case .Failure(let error):
print("ERROR : \(error.localizedDescription) - \(error.localizedRecoverySuggestion)")
completionHandler(nil, AppError.Unknown)
return
case .Success(_):
switch response.response!.statusCode {
case 200, 422:
break // wil be handle in responseJSON
case 401:
completionHandler(nil, AppError.AlamofireError)
case 500:
completionHandler(nil, AppError.ServerError)
default:
print("WARNING : status code \(response.response!.statusCode) not handled")
completionHandler(nil, AppError.Unknown)
}
}
})
.responseJSON { (response) -> Void in
print("\n - request : \(response.request!.HTTPMethod) \(response.request!.URLString)\n\n - has body : \(response.request!.HTTPBody != nil)\n\n - response : \(response.response)\n\n - result : \(response.result)\n")
switch response.result {
case .Failure(_):
completionHandler(nil, AppError.Unknown)
case .Success(let json):
if let responseURL = response.response {
switch responseURL.statusCode {
case 200:
completionHandler(json, nil)
case 201:
completionHandler(json, nil)
case 500:
completionHandler(nil, AppError.ServerError)
default:
print(responseURL)
print("WARNING : status code \(responseURL.statusCode) not handled")
completionHandler(nil, AppError.Unknown)
}
}
}
}
}
}
是的,我是sur我打開'refresh_token'值,令牌必須過期更改或我可以刷新令牌時,我打電話給終點? –
你介意分享更多的代碼嗎?沒有更多信息很難協助。 – faarwa
我添加了最佳理解的代碼 –