[英]Can any one tell me why i am getting Bad authentication error while executing this code(Swift)?
I am using Fabric SDK to add the twitter login button in my app....... i add the authentication header in my URL but still it is showing Bad authentication error while executing. 我正在使用Fabric SDK在我的应用程序中添加twitter登录按钮。......我在URL中添加了身份验证标头,但在执行时仍显示错误的身份验证错误。 Suggest me how to add Header in the URL in Swift. 建议我如何在Swift中的URL中添加标头。
let twitter = Twitter.sharedInstance()
let oauthSigning = TWTROAuthSigning(authConfig:twitter.authConfig, authSession:twitter.session())
let authHeaders = oauthSigning.OAuthEchoHeadersToVerifyCredentials()
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.twitter.com/1.1/search/tweets.json?q=Himan_dhawan")!)
request.allHTTPHeaderFields = authHeaders
println(request)
var session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if((error) != nil) {
println(error.localizedDescription)
}
var strData = NSString(data: data, encoding: NSASCIIStringEncoding)
println(strData)
})
task.resume()
It's to do with the way that you're setting the headers on the request. 这与您在请求上设置标头的方式有关。
The Fabric doc's don't quite give you the full picture about creating the OAuth signing headers when wanting to use your own NSMutableURLRequest. 当想要使用自己的NSMutableURLRequest时,Fabric文档并没有为您提供有关创建OAuth签名标头的完整信息。
let authHeaders = oauthSigning.OAuthEchoHeadersToVerifyCredentials()
The return [NSObject : AnyObject]! 返回[NSObject:AnyObject]! dictionary gives you the values you need for the request. 字典为您提供了请求所需的值。 However, what it provides for the headers are different to what needs to be sent with the NSMutableURLRequest. 但是,它为标头提供的内容与需要使用NSMutableURLRequest发送的内容不同。
This is how you should be setting the headers for this request: 这是您应该为此请求设置标题的方式:
let twitter = Twitter.sharedInstance()
let oauthSigning = TWTROAuthSigning(authConfig:twitter.authConfig, authSession:twitter.session())
let authHeaders = oauthSigning.OAuthEchoHeadersToVerifyCredentials()
let mutableUrlWithUsableUrlAddress = NSMutableURLRequest(URL: usableUrlForRequest)
mutableUrlWithUsableUrlAddress.addValue(authHeaders[TWTROAuthEchoAuthorizationHeaderKey] as? String, forHTTPHeaderField: "Authorization")
This sets the required Authorisation Key as a value for the "Authorization" header on the request, opposed to when you pass in the authHeaders dictionary, it gets set for "X-Verify-Credentials-Authorization". 这会将所需的授权密钥设置为请求中“ Authorization”标头的值,与您传入authHeaders字典时相反,它被设置为“ X-Verify-Credentials-Authorization”。
The Fabric doc's do go into this, but it's slightly more tucked away than it should be. Fabric文档确实可以解决这个问题,但是比实际情况要复杂得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.