iOS - Alamofire v2 基本身份验证不工作
iOS - Alamofire v2 Basic Auth not working
所以我向 Bing Image Search 发送了一个基本的身份验证请求以获取一些图像数据,它工作得很好,直到我更新到最新版本的 Alamofire(1.3 -> 2.0. 2),我不得不这样做,因为 1.3 甚至不兼容 XCode 7.
无论如何,这是我的代码:
let credentials = ":\(Settings.bingApiKey)"
let plainText = credentials.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let base64 = plainText!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
manager = Alamofire.Manager.sharedInstance
manager!.session.configuration.HTTPAdditionalHeaders = [
"Authorization": "Basic \(base64)"
]
let url = NSURL(string: Settings.bingImageApi + "&Query=" + keyword + "&$top=15&$skip=" + String(skip))!
manager!
.request(.POST, url, parameters: nil, encoding: .JSON)
.responseJSON { request, response, result in
...
我收到错误消息:
FAILURE: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}
The authorization type you provided is not supported. Only Basic and OAuth are supported
我在从 Alamofire 1.x 移动到 2.x 时遇到了同样的问题。
我发现的一个解决方法(并且有效)是在执行请求时传递 headers:
let headers = ["Authorization": "Basic \(base64)"]
Alamofire.request(.POST, url, parameters: nil, encoding: .JSON, headers: headers)
有关详细信息,您可以查看 documentation。
请阅读这里http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/
"App Transport Security (ATS) lets an app add a declaration to its Info.plist file that specifies the domains with which it needs secure communication. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one."
错误的第一部分是由于您没有收到有效的 JSON 响应。您可以使用 response
、responseData
或 responseString
来帮助调试。
错误的第二部分是由于您设置 header 的方式造成的。在创建 session 配置后,您不能设置 Authorization
header。您可以创建自己的 session 配置和自己的 Manager
,也可以在 request
.
中传递 Authorization
header
所以我向 Bing Image Search 发送了一个基本的身份验证请求以获取一些图像数据,它工作得很好,直到我更新到最新版本的 Alamofire(1.3 -> 2.0. 2),我不得不这样做,因为 1.3 甚至不兼容 XCode 7.
无论如何,这是我的代码:
let credentials = ":\(Settings.bingApiKey)"
let plainText = credentials.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let base64 = plainText!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
manager = Alamofire.Manager.sharedInstance
manager!.session.configuration.HTTPAdditionalHeaders = [
"Authorization": "Basic \(base64)"
]
let url = NSURL(string: Settings.bingImageApi + "&Query=" + keyword + "&$top=15&$skip=" + String(skip))!
manager!
.request(.POST, url, parameters: nil, encoding: .JSON)
.responseJSON { request, response, result in
...
我收到错误消息:
FAILURE: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.} The authorization type you provided is not supported. Only Basic and OAuth are supported
我在从 Alamofire 1.x 移动到 2.x 时遇到了同样的问题。
我发现的一个解决方法(并且有效)是在执行请求时传递 headers:
let headers = ["Authorization": "Basic \(base64)"]
Alamofire.request(.POST, url, parameters: nil, encoding: .JSON, headers: headers)
有关详细信息,您可以查看 documentation。
请阅读这里http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/ "App Transport Security (ATS) lets an app add a declaration to its Info.plist file that specifies the domains with which it needs secure communication. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one."
错误的第一部分是由于您没有收到有效的 JSON 响应。您可以使用 response
、responseData
或 responseString
来帮助调试。
错误的第二部分是由于您设置 header 的方式造成的。在创建 session 配置后,您不能设置 Authorization
header。您可以创建自己的 session 配置和自己的 Manager
,也可以在 request
.
Authorization
header