6

Android: I am trying the following xAuth example for android share.

xAuth Authentication for Twitter Share in Android?

System.setProperty("twitter4j.oauth.consumerKey", "your token");
System.setProperty("twitter4j.oauth.consumerSecret", "your token secret");

Twitter twitter = new TwitterFactory().getInstance(login, password);

AccessToken accessToken = twitter.getOAuthAccessToken();
//Then you must save your Token and Token secret from AccesToken

if (mAccessToken != null) {
    if (mAccessToken.getToken() != null && mAccessToken.getTokenSecret() != null) {
        saveAccessToken(mAccessToken.getToken(), mAccessToken.getTokenSecret());
    }
}

I am having IllegalStateException

Exception Msg: java.lang.IllegalStateException: OAuth consumer key/secret combination not supplied

at the following line

AccessToken accessToken = twitter.getOAuthAccessToken();

I made changes some thing like following

Twitter twitter = new TwitterFactory().getInstance("login", "pass");
            twitter.setOAuthConsumer(getString(R.string.twtAPIKey), getString(R.string.twtSecret));
            AccessToken mAccessToken = twitter.getOAuthAccessToken();

and again I got exception Exception Msg: java.lang.IllegalStateException: Basic authenticated instance.

Any working example of xAuth ?

Community
  • 1
  • 1
d-man
  • 55,965
  • 82
  • 204
  • 290
  • Are you sure your consumerKey and consumerSecret are correct? Also, I don't think this would give such an exception, but have they enabled XAuth for your application at Twitter? – DonSteep Aug 20 '10 at 08:15

2 Answers2

5

I ' ve used following


ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

     configurationBuilder.setOAuthConsumerKey(Consumer__Key);
     configurationBuilder.setOAuthConsumerSecret(Consumer_Secret);
     Configuration configuration = configurationBuilder.build();

     Twitter twitter = new TwitterFactory(configuration).getInstance("username","password"); 

     AccessToken token = twitter.getOAuthAccessToken();
     System.out.println("Access Token " +token );

     String name = token.getScreenName();
     System.out.println("Screen Name" +name);

     PrintWriter out= response.getWriter();
     System.out.println(token);

And successfully login to Twitter using Android app using xauth

Stephan
  • 40,082
  • 60
  • 228
  • 319
success_anil
  • 3,634
  • 3
  • 25
  • 31
  • 1
    I tried above code it is giving following exception message The screen name / password combination seems to be invalid. – d-man Aug 20 '10 at 06:23
  • @Faisal Khan Hi Have you got your keys enable for xAuth from Twitte. Probably this could be the reason for the error. – success_anil Oct 27 '10 at 04:49
  • Yes we are have to request twitter guys to have special xauth account and keys. – d-man Oct 27 '10 at 05:01
2

Version 2.2.2 of twitter4j as a slight change. This works:

Configuration configuration = new ConfigurationBuilder()
.setOAuthConsumerKey("your_customer_key")
.setOAuthConsumerSecret("your_customer_secret")
.build();

Twitter twitter = new TwitterFactory(configuration).getInstance(new BasicAuthorization(username, password)); // yes, use "BasicAuthorization" although that seems strange

AccessToken token = twitter.getOAuthAccessToken();
Log.d(TAG, "Access token: " + token.getToken());
Log.d(TAG, "Access token secret: " + token.getTokenSecret());

Remember that your twitter client application needs to be authorized to use xAuth before this works.

Randy Sugianto 'Yuku'
  • 68,307
  • 56
  • 174
  • 223