Alamofire不发送当前标题(swift)

时间:2016-02-10 02:44:12

标签: ios swift alamofire

我一直收到“error_type”:“OAuthException”,“code”:“41”当我使用alamofire时,甚至当我通过服务器时,我从头部授权之前获得了数据。我认为它继续发送相同的标题,如何确保alamofire发送当前标题?

    let headers = ["Authorization" : "\(AccessToken) \(TokenType)"]
    print(headers)

    Alamofire.request(.GET, "url/profile/", headers: headers, encoding: .JSON).responseJSON { response in
        switch response.result {}

修改 首先,我使用登录API

    let parameters = [
        "client_id": "\(Constant.clientId)",
        "client_secret": "\(Constant.clientSecret)",
        "response_type": "\(Constant.responseType)",
        "scope" : "\(Constant.scope)",
        "redirect_uri": "\(Constant.redirect)",
        "email": "\(email!)",
        "password": "\(pass!)"

    ]

    print(parameters)

    Alamofire.request(.POST, "http://url/login/", parameters: parameters, encoding: .JSON).responseJSON { response in
        switch response.result {
        case .Success:

            if let value = response.result.value {
                let json = JSON(value)
                print("JSON: \(json)")
                let accessToken = json["access_token"].string!
                let refreshToken = json["refresh_token"].string
                let tokenType = json["token_type"].string!
                let expiresIn = json["expires_in"].string
 }

然后,我使用accessTokentokenType进行授权

 if(refreshToken != nil)
                {
 let headersCust = ["Authorization" : "\(accessToken) \(tokenType)"]
                    print(headersCust)
Alamofire.request(.GET, "http://goodies.co.id/api/v1/customer/profile/", headers: headersCust, encoding: .JSON).responseJSON { response in {}

2 个答案:

答案 0 :(得分:3)

通常这个问题是由 npm start 引起的。使用诸如 Proxyman 之类的网络调试工具可以帮助您了解是否属于这种情况;如果是这样:

这是 Alamofire 5(AF 5) 解决方案:

Redirection

答案 1 :(得分:0)

我希望您使用的是最新的Alamofire,所以这里是.GET请求的代码:

let headers = [
    "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
    "Content-Type": "application/x-www-form-urlencoded"
]

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

参考:https://github.com/Alamofire/Alamofire 试试吧!并确保您正在发送正确的标题。