2017-07-02 63 views
1

全部 我無法通過swift在我的web服務器上進行身份驗證。 我可以通過Postman在我的網絡服務器上進行身份驗證。關於認證問題的swift狀態碼401

這是郵遞員 enter image description here

這是我的快捷。

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可能是錯的。你能教我什麼是錯的嗎?

回答

0

mutableUrlRequest.setValue(self.tokenUse, forHTTPHeaderField: "Authorization")中,您的self.tokenUse應該是您的clientID。如果這是正確的,爲什麼你的tokenUse有「令牌」前綴?

試試這個

let headers: HTTPHeaders = [ 
    "Authorization": self.tokenUse, 
    "Accept": "application/json" 
] 

Alamofire.request("http://ec2-52-79-171-171.ap-northeast-2.compute.amazonaws.com:8000/api/user/ryulstory", headers: headers).responseJSON { response in 

    debugPrint(response) 
} 
+1

我認爲HTTPheaderField與key(Authorization)相同,self.tokenUse與Postman上的值相同。所以在郵遞員的價值,它需要令牌前綴。 –

+0

當你使用Alamofire時,你可以用我編輯的答案再試一次。 – Lawliet

0

你插入App Transport SecurityAllow Arbitrary LoadsYES?進入你的Info.plist?此plist修改允許Apple傳輸安全規則通過200 HTTP服務器提出請求。

+0

是的,我已經做到了。但有401錯誤。 –