[英]Can't figure out why I am getting “Class ViewController has no initializers” Error in Swift iOS App Code
[英]Can any one tell me why i am getting Bad authentication error while executing this code(Swift)?
我正在使用Fabric SDK在我的應用程序中添加twitter登錄按鈕。......我在URL中添加了身份驗證標頭,但在執行時仍顯示錯誤的身份驗證錯誤。 建議我如何在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()
這與您在請求上設置標頭的方式有關。
當想要使用自己的NSMutableURLRequest時,Fabric文檔並沒有為您提供有關創建OAuth簽名標頭的完整信息。
let authHeaders = oauthSigning.OAuthEchoHeadersToVerifyCredentials()
返回[NSObject:AnyObject]! 字典為您提供了請求所需的值。 但是,它為標頭提供的內容與需要使用NSMutableURLRequest發送的內容不同。
這是您應該為此請求設置標題的方式:
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")
這會將所需的授權密鑰設置為請求中“ Authorization”標頭的值,與您傳入authHeaders字典時相反,它被設置為“ X-Verify-Credentials-Authorization”。
Fabric文檔確實可以解決這個問題,但是比實際情況要復雜得多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.