简体   繁体   English

谁能告诉我为什么我在执行此代码时遇到错误的身份验证错误(Swift)?

[英]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.

相关问题 无法弄清楚为什么我在Swift iOS应用程序代码中获得“Class ViewController没有初始化程序”错误 - Can't figure out why I am getting “Class ViewController has no initializers” Error in Swift iOS App Code Swift:我在Firebase移动身份验证中遇到错误 - Swift: I am getting error in firebase mobile authentication 为什么在此简单代码上出现“错误访问”异常? - Why Am I getting Bad Access exception on this simple code? 我在 Swift 中收到 Beams 身份验证令牌错误 - I am getting Beams Authentication Token Error in Swift 谁能告诉我 iOS 中的这些手势名称。 任何好的示例代码都会有所帮助 - Can any one tell me these gesture name's in iOS . Any good example code will be helpful 在Swift中设置GKMatchmakerViewControllerDelegate时出现错误吗? - While am I getting an error while setting up GKMatchmakerViewControllerDelegate in Swift? 为什么在命名 Swift 枚举大小写时出现“不是整数文字中的有效数字”错误? - Why am I getting 'is not a valid digit in integer literal' error while naming Swift enum case? 为什么我可以得到405个时间估计代码,却可以得到200个产品类型代码 - Why am I getting 405 code for Time Estimates while I can get 200 code for Product Types 我在调用API时遇到此错误-“线程1:EXC_BAD _INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0 * 0)” - I am getting this error while calling an API - “Thread 1: EXC_BAD _INSTRUCTION(code = EXC_1386_INVOP,subcode = 0*0)” 为什么我在 Xcode 10.1 中出现 swift 编译器错误 - Why I am getting swift compiler error in Xcode 10.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM