Twitter4j authentication credentials are missing

Viewed 13257

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 twitterPostWall(){

    try {


        //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 = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);

        try {
            RequestToken requestToken = twitter.getOAuthRequestToken();

            Log.e("Request token: ", "" + requestToken.getToken());
            Log.e("Request token secret: ", "" + requestToken.getTokenSecret());
            AccessToken accessToken = null;


        }

        catch (IllegalStateException ie) {

            if (!twitter.getAuthorization().isEnabled()) {
                Log.e("OAuth consumer key/secret is not set.", "");
            }
        }


        Status status = twitter.updateStatus(postLink);
        Log.e("Successfully updated the status to [", ""  + status.getText() + "].");

    }

    catch (TwitterException te) {
        Log.e("TWEET FAILED", "");

    }
}

I always get this error message from Twitter4j: java.lang.IllegalStateException: Authentication credentials are missing. See http://twitter4j.org/en/configuration.html for the detail. But as you can see I'm using builder to set my key. Can someone help me to fix it please? thanks.

2 Answers

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

Related