Alamofire 的身份验证一直失败
Authentication keeps failing with Alamofire
我最近将我的项目迁移到 Swift 2.0,但出现了很多错误。
我修复了大部分,但是这个一直出错。
我正在使用 Alamofire 调用 Bing 搜索 API,如下所示,我收到一条错误消息,提示 "Error Domain=NSCocoaErrorDomain Code=3840 " 字符 0 周围的值无效。” UserInfo={NSDebugDescription=字符周围的值无效0.}"
我理解这意味着身份验证失败。
有人可以告诉我如何解决这个问题吗?
let percentedKeyword = searchKey.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
Let ulrStr: String = "https://api.datamarket.azure.com/Bing/Search/v1/News" + "? Query=" + percentedKeyword! + "&$top=10&$format=JSON"
let credentials = ":\(bingApiKey)"
let plainText = credentials.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let base64 = plainText!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
let headers = ["Authorization": "Basic \(base64)"]
Alamofire.request(.GET, urlStr, headers: headers)
.responseJSON { request, response, data in
switch data {
case Result.Success(let receivedValue):
self.bingJson = JSON(receivedValue)
case Result.Failure(_, let error as NSError):
print(error)
default:
print("do nothing")
}
}
Xcode 版本 7.0
Alamofire 版本 2.0.2
[更新]
我尝试了 urlStr("https://api.datamarket.azure.com/Bing/Search/News?Query=%E4%B8%AD%E5%9B%BD&$top=10&$format=JSON") 网络浏览器。
它要求我输入用户名和密码,所以我输入 apiKey 作为密码,并按照 Microsoft doc
的说明将用户名留空
我明白了 error:Parameter: 查询不是字符串类型
根据Whosebug,这是因为关键字没有百分比,但我在关键字字符串中添加了百分比...
原来我搜索关键词需要单引号。
let percentedKeyword = searchKey.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
let urlStr: String = "https://api.datamarket.azure.com/Bing/Search/News" + "?Query=" + "'" + percentedKeyword! + "'" + "&$top=10&$format=JSON"
let credentials = ":\(bingApiKey)"
let plainText = credentials.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let base64 = plainText!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
let headers = ["Authorization": "Basic \(base64)"]
Alamofire.request(.GET, urlStr, headers: headers)
.responseJSON { request, response, data in
switch data {
case Result.Success(let receivedValue):
self.bingJson = JSON(receivedValue)
case Result.Failure(_, let error as NSError):
print(error)
default:
print("do nothing")
}
}
}
我最近将我的项目迁移到 Swift 2.0,但出现了很多错误。 我修复了大部分,但是这个一直出错。
我正在使用 Alamofire 调用 Bing 搜索 API,如下所示,我收到一条错误消息,提示 "Error Domain=NSCocoaErrorDomain Code=3840 " 字符 0 周围的值无效。” UserInfo={NSDebugDescription=字符周围的值无效0.}"
我理解这意味着身份验证失败。 有人可以告诉我如何解决这个问题吗?
let percentedKeyword = searchKey.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
Let ulrStr: String = "https://api.datamarket.azure.com/Bing/Search/v1/News" + "? Query=" + percentedKeyword! + "&$top=10&$format=JSON"
let credentials = ":\(bingApiKey)"
let plainText = credentials.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let base64 = plainText!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
let headers = ["Authorization": "Basic \(base64)"]
Alamofire.request(.GET, urlStr, headers: headers)
.responseJSON { request, response, data in
switch data {
case Result.Success(let receivedValue):
self.bingJson = JSON(receivedValue)
case Result.Failure(_, let error as NSError):
print(error)
default:
print("do nothing")
}
}
Xcode 版本 7.0 Alamofire 版本 2.0.2
[更新]
我尝试了 urlStr("https://api.datamarket.azure.com/Bing/Search/News?Query=%E4%B8%AD%E5%9B%BD&$top=10&$format=JSON") 网络浏览器。 它要求我输入用户名和密码,所以我输入 apiKey 作为密码,并按照 Microsoft doc
的说明将用户名留空我明白了 error:Parameter: 查询不是字符串类型
根据Whosebug,这是因为关键字没有百分比,但我在关键字字符串中添加了百分比...
原来我搜索关键词需要单引号。
let percentedKeyword = searchKey.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
let urlStr: String = "https://api.datamarket.azure.com/Bing/Search/News" + "?Query=" + "'" + percentedKeyword! + "'" + "&$top=10&$format=JSON"
let credentials = ":\(bingApiKey)"
let plainText = credentials.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let base64 = plainText!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
let headers = ["Authorization": "Basic \(base64)"]
Alamofire.request(.GET, urlStr, headers: headers)
.responseJSON { request, response, data in
switch data {
case Result.Success(let receivedValue):
self.bingJson = JSON(receivedValue)
case Result.Failure(_, let error as NSError):
print(error)
default:
print("do nothing")
}
}
}