swiftyjson responsedata objective example ios swift3 alamofire ios10

ios - responsedata - ¿Cómo pasar el token de acceso a Alamofire?



swiftyjson alamofire (3)

Estoy tratando de pasar token de acceso en Alamofire pero confundirme en varios métodos alrededor de la web.

A continuación hay métodos que debemos usar.

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)") } }

O

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

O

let headerss = [ "Authorization": tokenString]

O

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

¿Cuál es la forma correcta de pasar el token de acceso?


¿Has probado esto? Está disponible en la documentación de Alamofire:

let headers: HTTPHeaders = [ "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", "Accept": "application/json" ] Alamofire.request("https://httpbin.org/headers", headers: headers).responseJSON { response in debugPrint(response) }

Otro ejemplo es:

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) }

Una forma más es:

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) }


Estoy usando se ven así:

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)"]

O

let headers = [ "Authorization" : token ]


Se puede hacer utilizando Alamofire de la siguiente manera:

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 }