Alamofire不会向eventbrite发送授权标头

时间:2017-05-30 14:58:16

标签: ios swift oauth-2.0 alamofire eventbrite

我试图使用Alamofire查询eventbrite,但结果形式出现错误

[Result]: SUCCESS: {
    error = "NO_AUTH";
    "error_description" = "An OAuth token is required for all requests";
    "status_code" = 401;
}

我的第一个变种

 func getAdditionalInfo(_ urlPath: String) {
        DispatchQueue.global(qos: .background).async {
            let headers: HTTPHeaders = [
            "Authorization" : "Bearer 34832udsfsdjkfds" // 34832udsfsdjkfds is fake 
            ]

            Alamofire.request(urlPath, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers).responseJSON(completionHandler: { (response) in
                debugPrint("EventbriteAPIManager response", response)
                if response.result.error == nil {

                } else {
                    guard let error = response.result.error else { return }
                    debugPrint(error)
                }
            })
        }
    }

我的第二个变种

func getAdditionalInfo(_ urlPath: String) {
    DispatchQueue.global(qos: .background).async {
        let sessionManager = SessionManager.default
        sessionManager.session.configuration.httpAdditionalHeaders = [
            "Authorization" : "Bearer 34832udsfsdjkfds" // 34832udsfsdjkfds is fake 
        ]

        request(urlPath, method: .get, parameters: nil, encoding: URLEncoding.default).responseJSON(completionHandler: { (response) in
            debugPrint("EventbriteAPIManager response", response)
            if response.result.error == nil {

            } else {
                guard let error = response.result.error else { return }
                debugPrint(error)
            }
        })
    }
}

我尝试使用Postman发出此请求,一切正常。我该如何解决?感谢。

1 个答案:

答案 0 :(得分:0)

我改变了我的要求,它对我有用。问题是不应该将eventbrite令牌发送到头部,而是添加到URL(如果你在邮递员中测试它最有趣,那么如果将令牌发送到头部则它可以工作)。

private func getEventbriteLocation(_ urlPath: String, success: ((_ eventbriteModel: EventbriteLocationModel) -> Void)?, fail: ((_ error: Error) -> Void)?) {
    DispatchQueue.global(qos: .background).async {
        guard let BackendTokensModel = BackendAPIManager.shared.BackendTokensModel else {
            BackendAPIManager.shared.getTokens(success: { (tokenModel) in
                self.getEventbriteLocation(urlPath, success: success, fail: fail)
            }, fail: nil)
            return
        }

        let rightURL = urlPath.appending("/?token=\(BackendTokensModel.eventbrite)")
        AlamofireManager.shared.request(rightURL, method: .get).responseJSON(completionHandler: { (response) in
            if response.result.error == nil {
                guard let json = response.result.value as? [String : Any] else { return }
                guard let eventbriteModel = Mapper<EventbriteLocationModel>().map(JSON: json) else { return }
                success?(eventbriteModel)
            } else {
                guard let error = response.result.error else { return }
                debugPrint(error)
                fail?(error)
            }
        })
    }
}