2017-03-06 14 views
0

Alamofireとのリクエストを受けて403が手に入りました。サーバーから取得するエラーメッセージは、認証資格情報が提供されていません。 要求に応じてこの認証ヘッダーが見つからないというこの問題に従っています。認証ヘッダーに自分のアクセストークンを送信すると確信しています。 WireSharkは、要求が認証ヘッダーと共に送信されることを示します。だから私はそれを修正する方法がわかりません。ここでは、コードスニペットは次のとおりです。403 Alamofireのリクエストに応じて認証資格情報が提供されない

var authenticatedJsonHeaders: [String: String] { 
     get { 
      return [ 
       "Content-Type": "application/json", 
       "Accept": "application/json", 
       "Authorization": "\(accessTokenAuthType.rawValue) \(accessTokenString)" 
      ] 
     } 
    } 

..... 

return Alamofire.request(urlString, method:method, parameters: nil, encoding: encoding, headers: authenticatedJsonHeaders) 
     .response { response in 

      let currentUserId = ApplicationState.sharedInstance.currentUserManager.getCurrentUserProfileId() 

      if(currentUserId == 0){ //if current user exists, do not sign the user out! 
       print("bye user..") 
       self.signOutUnauthorizedUser(response.response?.statusCode, urlOrSuffix: urlString) 
      } 

      self.checkResponseForSharevilleAlertMessage(response.response) 

      if !hasResponseObject { 
       if /* 
let response = response,*/ (response.response?.statusCode)! < 400 && response.error == nil { 
        print("success for statuscode: \(response.response?.statusCode) url: \(urlString)") 
        success(nil) 
       } else { 
        sendApiErrorWithUrlIfStatusCodeIsNot204(self.getUrlFromSuffix(urlString), statusCode: response.response?.statusCode) 
        print("fail for statuscode: \(response.response?.statusCode ?? nil) url: \(self.getUrlFromSuffix(urlString))") 
        print("request header") 
        print(response.request ?? "none" 
        failure(response.error) 
       } 
      } 
     } 
     .responseJSON { data in 
      if hasResponseObject { 
       if let value = data.result.value, data.response != nil && data.response!.statusCode < 400 && data.result.error == nil { 
        print("success for statuscode: \(data.response!.statusCode) url: \(urlString)") 
        success(value as AnyObject?) 
       } else { 
        sendApiErrorWithUrlIfStatusCodeIsNot204(urlString, statusCode: data.response?.statusCode) 
        print("fail for statuscode: \(data.response?.statusCode) url: \(urlString)") 
        failure(data.result.error) 
       } 
      } 
     } 
+0

私の推測ですか?あなたはデフォルトの '(NS)URLSession'をデフォルト'(NS)URLSessionConfiguration'で使用しています。これは "Authorization"、 "Connection"、 "Host"、 "WWW-Authenticate"というキーのヘッダーを変更することを拒否することができます。そこを見ることができます:https://github.com/Alamofire/Alamofire#http-headers&https://github.com/Alamofire/Alamofire#session-manager – Larme

答えて

0

私はあなたが

.validate()

を試みるべきだと思います。 request()とresponse()の間にある。あなたのalamofireリクエストの電話で

Alamofire.request(urlString, method:method, parameters: nil, encoding: encoding, headers: authenticatedJsonHeaders) 
     .validate().response { // } 
関連する問題