Search code examples
androidauthenticationtwittertwitter4j

Update Status in Twitter via twitter4j Library - 401 Authontication Credentials... error


Hi I am trying to implement "status update" via twitter4j library.

However it am always getting the same error:

401:Authentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect.

Ensure that you have set valid consumer key/secret, access token/secret, and the system clock in in sync.

TwitterException{exceptionCode=[6b80c41c-1bd1da85], statusCode=401, retryAfter=-1, rateLimitStatus=null, featureSpecificRateLimitStatus=null, version=2.2.5-SNAPSHOT(build: 5f9c44cd0e0c1972f8d17653a226b9b6a3392ac2)} ...

Below you can find the code piece:

ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
        .setOAuthConsumerKey(cons_key)
        .setOAuthConsumerSecret(cons_secret)
        .setOAuthAccessToken(acc_token)
        .setOAuthAccessTokenSecret(acc_secret);

        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();
        twitter4j.Status status;
        try {
            status = twitter.updateStatus(params[0]);
            if (status.getId() == 0) {
                System.out.println("Error occured while posting tweets to twitter");
            }
        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

I am pretty sure about I have the valid consumer/access tokens/secrets.

I don't know how to sync the system clock.

Do you have an idea what is the actual problem behind this error? I couldn't find any useful answer on internet.

Any help would be appreciated.. Thanks in advance!


Solution

  • You can try few things -

    1. Regenerate the authentication keys for your registered app by logging in to https://apps.twitter.com.
    2. Also ensure your registered application has Read/Write access to be able to post an status update.
    3. I am not sure about clock sync on android, but for linux/Windows you can check which NTP server to point your system to.

    Also use latest version of twitter4j.