全部 我無法通過swift在我的web服務器上進行身份驗證。 我可以通過Postman在我的網絡服務器上進行身份驗證。關於認證問題的swift狀態碼401
這是我的快捷。
class ViewController: UIViewController {
let authLoginUrl = "http://ec2-52-79-171-171.ap-northeast-2.compute.amazonaws.com:8000/rest-auth/login/"
let keychain = Keychain(service: "wanote")
let tokenUse = "Token 869463aacc84f0fb9edfb251fa73b352fc52a4d5"
let projectUrl = "http://ec2-52-79-171-171.ap-northeast-2.compute.amazonaws.com:8000/api/user/ryulstory"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let Purl = URL(string: self.projectUrl){
var mutableUrlRequest = URLRequest(url: Purl)
mutableUrlRequest.setValue(self.tokenUse, forHTTPHeaderField: "Authorization")
mutableUrlRequest.httpMethod = "GET"
_ = Alamofire.SessionManager.default.request(mutableUrlRequest)
.responseJSON{ response in
print(response.request)
print(response.response)
print(response.data)
print(response.result)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我覺得setvalue可能是錯的。你能教我什麼是錯的嗎?
我認爲HTTPheaderField與key(Authorization)相同,self.tokenUse與Postman上的值相同。所以在郵遞員的價值,它需要令牌前綴。 –
當你使用Alamofire時,你可以用我編輯的答案再試一次。 – Lawliet