2016-11-25 84 views
3

我試圖在Alamofire中傳遞訪問令牌,但在圍繞Web的各種方法中引起混淆。如何將訪問令牌傳遞給Alamofire?

以下是我們需要使用的方法。

let todosEndpoint: String = "https:url......." 

let headers = [ 
      "Authorization": "Bearer \(token!)", 
      "Content-Type": "application/X-Access-Token" 
     ] 
     let Auth_header = [ "Authorization" : tokenString! ] 

     Alamofire.request(todosEndpoint, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: Auth_header) 
      .responseJSON { response in 
       print("response.request \(response.request)") // original URL request 
       print("response.response \(response.response)") // HTTP URL response 
       print("response.data \(response.data)")  // server data 
       print("response.result \(response.result)") 

       print("response \(response)") 

     } 

    } 

OR

let aManager = SessionManager() 
     aManager.session.configuration.httpAdditionalHeaders = [ 
      "Authorization": "Bearer tokenString"] 

OR

let headerss = [ 
      "Authorization": tokenString] 

OR

let aManager = SessionManager() 
      aManager.session.configuration.httpAdditionalHeaders = [ 
       "Authorization": "Basic tokenString"] 

什麼是通過訪問令牌有道?

+0

選擇之一。它工作?好,完成。它沒有工作?嘗試另一個。它有用嗎?好。完成。哦,一個也沒有工作?試試另一個。這有用嗎?不?我沒有選擇嗎?是。 Stackoverflow – zerohero

+0

你不是第一個工作? –

+0

不幸的是,非以上是爲我工作?所以我不確定我是否通過任何錯誤的方式傳遞令牌 – sss

回答

1

我使用的是這個樣子的: -

let username = "Tuser" 
let password = "Tpassword" 
let credentialData = "\(username):\(password)".data(using: String.Encoding.utf8)! 
let base64Credentials = credentialData.base64EncodedString(options: []) 
let headers = ["Authorization": "Basic \(base64Credentials)"] 

OR

let headers = [ "Authorization" : token ] 
+0

什麼是您的答案中的用戶名和密碼...是否需要這樣做?我只想傳遞訪問令牌 – sss

+0

後端(API)開發人員爲您提供用戶名和密碼。這稱爲基本認證。 –

+0

我正在登錄api成功獲取訪問令牌,我每次登錄api後都會傳遞...在AFNetworking中,我一直在做同樣的事情,並且從未要求用戶名和密碼來後臺team..is這是Alamofire需要的嗎? – sss

2

你有沒有試過,它Alamofire文檔中的可用:

let headers: HTTPHeaders = [ 
    "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", 
    "Accept": "application/json" 
] 

Alamofire.request("https://httpbin.org/headers", headers: headers).responseJSON { response in 
    debugPrint(response) 
} 

另一個例子是:

let user = "user" 
let password = "password" 

var headers: HTTPHeaders = [:] 

if let authorizationHeader = Request.authorizationHeader(user: user, password: password) { 
    headers[authorizationHeader.key] = authorizationHeader.value 
} 

Alamofire.request("https://httpbin.org/basic-auth/user/password", headers: headers) 
    .responseJSON { response in 
     debugPrint(response) 
    } 

另一種方式是:

let user = "user" 
let password = "password" 

let credential = URLCredential(user: user, password: password, persistence: .forSession) 

Alamofire.request("https://httpbin.org/basic-auth/\(user)/\(password)") 
    .authenticate(usingCredential: credential) 
    .responseJSON { response in 
     debugPrint(response) 
    } 
1

它可以通過以下方式使用Alamofire來完成:

let url: String = "https:url......." 
    var request = URLRequest(url: NSURL(string: url)! as URL) 

    // Your request method type Get or post etc according to your requirement 
    request.httpMethod = "POST" 

    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") 
    request.setValue("application/json", forHTTPHeaderField: "Content-Type") 

    // Your required parameter in case of Post request 
    let parameters: [String: Any] = ["paramter1": "vaue1"] 

    request.httpBody = try! JSONSerialization.data(withJSONObject: parameters) 

    Alamofire.request(request).responseJSON { (responseObject) -> Void in 

     // Your required functionality here    

    } 
相關問題