Getting single Tweet with TwitterKit/Fabric authentication failure

十年热恋 提交于 2020-01-16 02:49:29

问题


I'm trying to retrieve a single tweet and show it in my application via the TWTRTweetView provided in the TwitterKit. I've followed this Fabric guide and ended up with following code.

import UIKit
import TwitterKit


class SingleTweetViewController: UIViewController{


@IBOutlet weak var plainView: UIView!

override func viewDidLoad(){
    super.viewDidLoad()

    Twitter.sharedInstance().logInGuestWithCompletion { session, error in
        if let validSession = session {
            Twitter.sharedInstance().APIClient.loadTweetWithID("4831830029115392") { tweet, error in
                if let t = tweet {
                    let tweetView = TWTRTweetView(tweet: tweet)
                    tweetView.showActionButtons = true
                    self.plainView.addSubview(tweetView)
                } else {
                    println("Failed to load Tweet: \(error?.localizedDescription)")
                }
            }

        } else {
            println("Unable to login as guest: \(error.localizedDescription)")
            println(error?.localizedFailureReason)
        }
    }
}

The code generates these two errors due to authentication failure.

Unable to login as guest: Request failed: forbidden (403)
Optional("Twitter API error : Unable to verify your credentials (code 99)")

It is worth mentioning that the app successfully signs in to Twitter via the login button added following this guide. Does anyone have a clue how this error could be fixed? Am I missing some code here or is the issue related to Fabric?


回答1:


You have to initialize Fabric before trying to use it which is what you're doing in your example code.

For the initialization, follow instructions from the Fabric documentation site. In essence, you have add the following lines to your app delegate (in addition to importing TwitterKit):

Twitter.sharedInstance().startWithConsumerKey("your_key", consumerSecret: "your_secret")
Fabric.with([Twitter.sharedInstance()])

Then copy and paste you're consumer key and secret from fabric.io. Fabric should automatically generate these for you.




回答2:


I solved the problem using Fabric applications built in option "View tweet in application" found in the same menu as the "Add login button button" option. The Fabric application then inserts proper authentication keys into the Info.plist file. The code provided by Fabric application seems to do the same thing as the code given on Fabric docs but the result differs depending on which one you use. The two code samples look like this:

Code from Fabric docs:

Twitter.sharedInstance().logInGuestWithCompletion { session, error in
  if let validSession = session {
    Twitter.sharedInstance().APIClient.loadTweetWithID("20") { tweet, error in
      if let t = tweet {
        self.tweetView.configureWithTweet(t)
      } else {
        println("Failed to load Tweet: \(error.localizedDescription)")
      }
    }
  } else {
     println("Unable to login as guest: \(error.localizedDescription)")
  }
}

Code from Fabric application:

Twitter.sharedInstance().logInGuestWithCompletion { (session, error) in
        Twitter.sharedInstance().APIClient.loadTweetWithID("20") { (tweet, error) in
            self.view.addSubview(TWTRTweetView(tweet: tweet))
        }
    }

Running the first code will give an authentication error while the second one will load a tweet without issues. The if-statement if let validSession = session returns a false and an authentication error occurs. I couldn't find what the validSession is exactly but it probably compares two different sets of keys or something similar.



来源:https://stackoverflow.com/questions/32482903/getting-single-tweet-with-twitterkit-fabric-authentication-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!