环境:Swift 2,iOS 9
我已经推出了自己的HTTP网络库。它没有被打破,但我决定修理它并切换到Alamofire。
这是我的代码段:
case .HTTPTokenAuth: // Token Auth
let dictionary = Locksmith.loadDataForUserAccount("Auth_Token", inService: "KeyChainService")
let token = dictionary!.valueForKey("access_token")
var aManager = Manager.sharedInstance
aManager.session.configuration.HTTPAdditionalHeaders = ["Authorization": "Bearer \(token!)" ]
aManager.request(method, requestURL!, parameters: parameters, encoding: .JSON).response { request, response, data, error in
print("===== Request ================")
print(request!.allHTTPHeaderFields!)
print("===== Response ================")
print(response!.allHeaderFields)
print("===== Error ================")
print(error)
}
问题是由于某种原因,Oauth令牌没有进入标题。 (至少这似乎是问题,因为我在Request标题中看不到它。我收到以下请求/回复:
===== Request ================
["Content-Type": "application/json"]
===== Response ================
[Content-Type: application/json,
Www-Authenticate: Bearer realm="Doorkeeper",
error="invalid_token",
error_description="The access token is invalid",
Pragma: no-cache, X-Runtime: 0.002205,
X-Powered-By: Phusion Passenger 4.0.14, X-XSS-Protection: 1; mode=block, Server: nginx/1.6.2 + Phusion Passenger 4.0.14,
Transfer-Encoding: Identity,
Cache-Control: no-store,
Date: Tue, 01 Sep 2015 18:54:16 GMT,
X-Request-Id: 395d2154-5054-423c-a7bc-f7ef85e0cdbf,
Connection: keep-alive,
X-Content-Type-Options: nosniff,
X-UA-Compatible: chrome=1,
Status: 401 Unauthorized,
X-Frame-Options: SAMEORIGIN]
答案 0 :(得分:0)
我采用了更简单的方法:
case .HTTPTokenAuth: // Token Auth
let dictionary = Locksmith.loadDataForUserAccount("Auth_Token", inService: "KeyChainService")
let token = dictionary!.valueForKey("access_token")
let headers = ["Authorization":"Bearer \(token!)"]
Alamofire.request(method, requestURL!, parameters: parameters, headers: headers, encoding: .JSON)
.response { request, response, data, error in
print(request!.allHTTPHeaderFields!)
print(response)
print(error)
}