Search code examples
androidtwittertwitter4jxauth

TwitterException with xAuth while posting from Android using Twitter4j


I am using twitter4j-2.1.3.jar to make a simple post to twitter. The code I use is:

System.setProperty("twitter4j.oauth.consumerKey", TWITTER_CONSUMER_KEY);
System.setProperty("twitter4j.oauth.consumerSecret",TWITTER_SECRET_KEY); 
Twitter twitter = new TwitterFactory().getInstance(username,password);
AccessToken accessToken = twitter.getOAuthAccessToken();
twitter.updateStatus(": xAuth test");

The error message I get is:

 TwitterException{exceptionCode=[e07c50ee-0000d883 1ac06e3f-626a1296], statusCode=401,   retryAfter=0, rateLimitStatus=null, version=2.1.3}

   at twitter4j.http.OAuthAuthorization.getOAuthAccessToken(OAuthAuthorization.java:185)
   at twitter4j.TwitterOAuthSupportBaseImpl.getOAuthAccessToken(TwitterOAuthSupportBaseImpl.java:108)
   at twitter4j.Twitter.getOAuthAccessToken(Twitter.java:54)
   at com.sfway.detailpage.CheckinTab$16$1.run(CheckinTab.java:640)
   TwitterException{exceptionCode=[1ac06e3f-626a1296], statusCode=401, retryAfter=0, rateLimitStatus=null, version=2.1.3}
   at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:307)
   at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:72)
   at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:103)
   at twitter4j.http.OAuthAuthorization.getOAuthAccessToken(OAuthAuthorization.java:178)

I tried an other way like this:

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); 
configurationBuilder.setOAuthConsumerSecret(TWITTER_SECRET_KEY);
Configuration configuration = configurationBuilder.build();
twitter = new TwitterFactory(configuration).getInstance(username,password);
AccessToken token = twitter.getOAuthAccessToken();
twitter.updateStatus(": xAuth test");

In both methods I get the error at "twitter.getOAuthAccessToken()" line. Please let me know what is the wrong. I have registered my app exactly 14 hrs before, to get the secret and consumer key. Is this something to do with approval of my app for xAuth use, if yes how can I know whether they have enabled XAuth for my application at Twitter.


Solution

  • Have you emailed Twitter support to get them to turn on xAuth for your application?

    Twitter only want xAuth to be used by desktop and mobile applications, so registered applications have xAuth disabled by default, and you need someone at Twitter to turn it on for you. If you application doesn't have xAuth enabled, you get a 401.

    To get xAuth enabled, send an email to api@twitter.com with the name of your application and the reason it needs xAuth instead of plain old OAuth (eg. it's not appropriate to pop up a web browser in my desktop application).

    Thanks Dhrumil Shah