Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter4J Access token already available

I am receiving error messages while working with Twitter4J:

java.lang.IllegalStateException: Access token already available.
twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:112)
twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:276)
twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:269)
[...]

This exception is thrown while calling the method Twitter.getOAuthRequestToken(). I want to get the Authorization URL to authenticate the next user.

How am I possible to solve this problem? I only put the OAuthConsumerKey, the OAuthConsumerSecret, the OAuthAccessToken and the OAuthAccessTokenSecret to the Twitter4J properties. But how do I receive the authorization URL to authenticate a new user?

Thanks and greetings,

Martin

like image 467
Martin Bories Avatar asked Oct 20 '12 15:10

Martin Bories


2 Answers

Sorry.

I was setting an Access Token hard coded by the Configuration Builder.

Removed it, works now.

like image 128
Martin Bories Avatar answered Nov 13 '22 09:11

Martin Bories


You need to create a new instance of twitter and null it's accessToken and accessTokenSecret fields before requesting new access token.

ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
            .setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
            .setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
            .setOAuthAccessToken(null)
            .setOAuthAccessTokenSecret(null);
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
like image 24
kezoo Avatar answered Nov 13 '22 09:11

kezoo