Alamofire 不向 eventbrite 发送授权 header
Alamofire doesn’t send Authorization header to 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 发出此请求,一切正常。我该如何解决?谢谢
我更改了我的请求,它对我有效。问题是 eventbrite 令牌不应该发送到 headers,而是添加到 URL(如果你在邮递员中测试它最有趣,那么如果令牌发送到 headers).
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)
}
})
}
}
我正在尝试使用 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 发出此请求,一切正常。我该如何解决?谢谢
我更改了我的请求,它对我有效。问题是 eventbrite 令牌不应该发送到 headers,而是添加到 URL(如果你在邮递员中测试它最有趣,那么如果令牌发送到 headers).
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)
}
})
}
}