使用Basic Auth进行身份验证时遇到问题。我正在使用符合//Appends link
var link = svg.selectAll(".link")
.data(links)
.enter()
.append("g") // circle
.attr("class", "link");
//Append circle
link.append("polyline") //if "line" It not show path
.attr("class", "link-line")
.style("stroke-width", 1)
//.attr("fill", "Black")
//.style("stroke-width", function (d) { return Math.sqrt(d.value); })
.style("marker-mid", "url(#end)");
//Appends link text
link.append("text")
.attr("class", "link-label")
//.attr("font-family", "tahoma, verdana, sans-serif")
.attr("fill", "Black")
.style("font", "normal 10px tahoma")
.attr("dy", ".15em")
.attr("text-anchor", "middle")
.text(function(d) { return d.value; });
协议的标准枚举来构建我的请求。问题是,当我在枚举中手动设置授权标头时,如下所示:
URLRequestConvertible
我总是得到401未经授权的回复。 然而如果我使用 let user = ***
let password = ***
let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
mutableURLRequest.setValue("Basic \(base64Credentials)", forHTTPHeaderField: "Authorization")
回调设置密码,如下所示:
authenticate
它正确验证。我希望能够在符合 Alamofire.request(request)
.authenticate(user: "USERNAME_HERE", password: "PASSWORD_HERE")
.responseJSON { (response) -> Void in
print("JSON response \(response)")
completion(success: true, error: nil)
}
的枚举中手动设置它,而不是传递URLRequestConvertible
中的凭据。
我知道它正在使用authenticate
进行身份验证挑战,但我希望能够手动设置它。
以下是我的NSURLCredential
实施:
URLRequestConvertible
答案 0 :(得分:21)
在swift 3.0中
使用以下代码 -
let user = ***
let password = ***
let credentialData = "\(user):\(password)".data(using: String.Encoding.utf8)!
let base64Credentials = credentialData.base64EncodedString(options: [])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(customerURL,
method: .get,
parameters: nil,
encoding: URLEncoding.default,
headers:headers)
.validate()
.responseJSON { response in
if response.result.value != nil{
print(response)
}else{
}
}
答案 1 :(得分:15)
Alamofire.request(urlString, method: .get).authenticate(user: "username", password: "pwd").responseJSON
JUST认证
答案 2 :(得分:13)
您可以尝试以下代码:
let user = ***
let password = ***
let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.manager.request(.GET, stringURL,headers: headers, parameters: params as? [String : AnyObject])
.responseJSON { response in
if (response.result.error == nil){
success(data: response.result.value)
}else{
fail(error: response.result.error)
}
}
答案 3 :(得分:2)
最终找出问题所在。它最终成为URL中缺少的尾随斜杠。似乎Alamofire没有像AFNetworking那样处理它。我能够弄清楚记录请求并发现我们在实际请求中丢失了一些字节。
答案 4 :(得分:1)
Alamofire提供了一种比手动创建自己的标题更简单的方法。
来自" Basic Auth"的相关代码。第here节:
manager.request(.GET, "https://api.parse.com/1/classes/Spot/")
.authenticate(user: username, password: password)
.responseSpotsArray { response in
completionHandler(response.result)
}
答案 5 :(得分:0)
快捷键4
private func getHeaders() -> [String: String] {
let userName = "xxxx"
let password = "xxxx"
let credentialData = "\(userName):\(password)".data(using: .utf8)
guard let cred = credentialData else { return ["" : ""] }
let base64Credentials = cred.base64EncodedData(options: [])
guard let base64Date = Data(base64Encoded: base64Credentials) else { return ["" : ""] }
return ["Authorization": "Basic \(base64Date.base64EncodedString())"]
}