Twitter4j authentication credentials are missing

后端 未结 2 1386
夕颜
夕颜 2020-12-19 05:44

I would like to make a tweet with Twitter4j in my Android app. Here is my code:

 //TWITTER SHARE.
@Click (R.id. img_btn_twitter)
@Background
public void twit         


        
相关标签:
2条回答
  • 2020-12-19 06:13

    Problem is following lines.

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = new TwitterFactory().getInstance();
    

    You are passing the configuration to one TwitterFactory instance and using another TwitterFactory instance to get the Twitter instance.

    Hence, You are getting java.lang.IllegalStateException: Authentication credentials are missing

    I suggest you to modify your code as follows:

        //Twitter Conf.
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey(CONSUMER_KEY)
                .setOAuthConsumerSecret(CONSUMER_SECRET)
                .setOAuthAccessToken(ACCESS_KEY)
                .setOAuthAccessTokenSecret(ACCESS_SECRET);
    
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();
    

    And use this twitter instance. It will work.

    0 讨论(0)
  • 2020-12-19 06:13

    I was having issues with the configuration on Twitter4j because I was not providing the right configuration. So in order to fix it, I created the following function to establish my configuration to later be used in another function:

    public static void main(String args[]) throws Exception {
    
        TwitterServiceImpl impl = new TwitterServiceImpl();
        ResponseList<Status> resList = impl.getUserTimeLine("spacex");
    
        for (Status status : resList) {
            System.out.println(status.getCreatedAt() + ": " + status.getText());
        }
    }
    
    public ResponseList<Status> getUserTimeLine(String screenName) throws TwitterException {
        
        TwitterFactory twitterFactory = new TwitterFactory(getConfiguration().build());
        Twitter twitter = twitterFactory.getInstance();
        twitter.getAuthorization();
        Paging paging = new Paging(1, 10);
        twitter.getId();
        return twitter.getUserTimeline(screenName, paging);
    }
    
    public ConfigurationBuilder getConfiguration() {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
        .setOAuthConsumerKey("myConsumerKey")
        .setOAuthConsumerSecret("myConsumerSecret")
        .setOAuthAccessToken("myAccessToken")
        .setOAuthAccessTokenSecret("myAccessTokenSecret");
        return cb;
    }
    

    To get the required info, you must have a Twitter developer account, and to get the auth info of an app previously created go to: Projects and Apps.

    In the end, I was able to retrieve the data from a SpaceX account:

    Tue Nov 24 20:58:13 CST 2020: Falcon 9 launches Starlink to orbit – the seventh launch and landing of this booster https://twitter.com/SpaceX/status/1331431972430700545
    Tue Nov 24 20:29:36 CST 2020: Deployment of 60 Starlink satellites confirmed https://twitter.com/SpaceX/status/1331424769632215040
    Tue Nov 24 20:23:17 CST 2020: Falcon 9’s first stage lands on the Of Course I Still Love You droneship! https://twitter.com/SpaceX/status/1331423180431396864
    Tue Nov 24 20:14:20 CST 2020: Liftoff! https://twitter.com/SpaceX/status/1331420926450094080
    Tue Nov 24 20:02:38 CST 2020: Watch Falcon 9 launch 60 Starlink satellites ? https://www.spacex.com/launches/index.html  https://twitter.com/i/broadcasts/1ypKdgVXWgRxW
    Tue Nov 24 19:43:14 CST 2020: T-30 minutes until Falcon 9 launches its sixteenth Starlink mission. Webcast goes live ~15 minutes before liftoff https://www.spacex.com/launches/index.html
    Tue Nov 24 18:00:59 CST 2020: RT @elonmusk: Good Starship SN8 static fire! Aiming for first 15km / ~50k ft altitude flight next week. Goals are to test 3 engine ascent,…
    Mon Nov 23 15:45:38 CST 2020: Now targeting Tuesday, November 24 at 9:13 p.m. EST for Falcon 9’s launch of Starlink, when weather conditions in the recovery area should improve
    Sun Nov 22 20:45:13 CST 2020: Standing down from today’s launch of Starlink. Rocket and payload are healthy; teams will use additional time to complete data reviews and are now working toward backup opportunity on Monday, November 23 at 9:34 p.m. but keeping an eye on recovery weather
    Sat Nov 21 22:09:12 CST 2020: More Falcon 9 launch and landing photos ? https://www.flickr.com/photos/spacex https://twitter.com/SpaceX/status/1330362669837082624
    

    Where to get Auth Tokens for your app

    0 讨论(0)
提交回复
热议问题