我在Alamofire遇到了一些问题。我想做什么:
在用户名和&amp ;;时收到用户令牌密码是正确的。这适用于我的代码。 我现在需要做的是发送令牌以从API中检索数据。
curl -X GET -H "Authorization: Token 5f1c81fb59a7ddf3a2" http://myurl.com
这是我必须收到的令牌:
let parameters = [
"username" : usernameLabel.text!,
"password" : passwordLabel.text!
]
let credentialData = "\(usernameLabel.text):\(passwordLabel.text)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(.POST, requestString, parameters: parameters, encoding: .JSON, headers: headers)
.responseJSON { response in switch response.result {
case .Success(let JSON):
print("Success with JSON: \(JSON)")
let response = JSON as! NSDictionary
//example if there is an token
self.userToken = response.objectForKey("token") as! String?
print("here comes the userToken \(self.userToken)")
self.retrieveData()
debugPrint(response)
case .Failure(let error):
print("Request failed with error: \(error)")
}
这里我想发送另一个请求的令牌(检索JSON数据)
func retrieveData() {
let headers = ["Authorization": "\(self.userToken)"]
Alamofire.request(.GET, URL, encoding: .JSON, headers: headers)
.responseJSON { response in switch response.result {
case .Success(let JSON):
print("Success with JSON: \(JSON)")
let response = JSON as! NSDictionary
print(response)
debugPrint(response)
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
}
答案总是:detail =“未提供身份验证凭据。”;
但卷曲有效:/
我的retrieveData函数有什么问题?