Alex Rivera | Logout

Why the method getOAuthAccessToken always fire the exception in the twitter4j api?

Asked 2011-05-19T21:32:01.080
11

I'm following a lot of instructions to make a simple tweet from my app. I've already registered it on Twitter, but I just can't make a tweet. I can login, but not update my status. Here's the code to login

private void twitterLogOn() {
        Twitter twitter = new TwitterFactory().getInstance();
        try {

            twitter.setOAuthConsumer(consumerKey, consumerSecret);
            rToken = twitter.getOAuthRequestToken(myCallBack);
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(rToken.getAuthenticationURL())));
        } catch (IllegalStateException e) {
            // access token is already available, or consumer key/secret is not
            // set.
            if (!twitter.getAuthorization().isEnabled()) {
                System.out.println("OAuth consumer key/secret is not set.");
                finish();
            }
        } catch (Exception e) {
            Toast.makeText(Configuration.this,getString(R.string.networkError), Toast.LENGTH_SHORT).show();
        }
    }

That seems to work fine, but when I go back to my app after the login, this next code should be executed, ending always in the exception Toast.

public void onResume() {    
        super.onResume();

        Uri uri = getIntent().getData();

        if (uri != null) {    
            oauthVerifier = uri.getQueryParameter("oauth_verifier");

            try {    
                Twitter tt = new TwitterFactory().getInstance(); // Do I need this new twitter instance?
                tt.setOAuthConsumer(consumerKey, consumerSecret);
                AccessToken at = tt.getOAuthAccessToken(rToken, oauthVerifier); // Gives the error

                       // Do tweet here ...

                } catch (Exception e) {
                    Toast.makeText(Configuration.this, "Network Host not responding",Toast.LENGTH_SHORT).show();
                }
            }
    }

Any good hawk eye there that can tel

Edit
Report

1 Answer

21

I've gone an solved it myself, only took 4 hours!!

I'm not 100% sure on the reasoning, but I know the fix works.

Fix:

In your manifest for this Activity you need to make it a single instance:

 <activity
        android:name=".TwitterTweetActivity"
        android:launchMode="singleInstance">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="your-unique-schema-01-android" />
        </intent-filter>
    </activity>

Now when your Activity resumes you need to catch it coming back from the browser with onNewIntent() like so:

 @Override
protected void onResume() {
    super.onResume();
    dealWithTwitterResponse(getIntent());
}

Replaced with:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    dealWithTwitterResponse(intent);
}
 

And it should work!

Reasoning (correct me if I'm wrong):

So the problem is your rToken object isn't the same object it was when created...

When you create your Twitter instance and get the rToken object this is in your activity. The activity then goes into the background (onPause) whilst the browser comes up for the user to login.

When the activity is recreated the rToken object is different therefore this is why the getOAuthAccessToken() method throws the error.

Extra Credit:

I've just written a Tutorial to match: How to send Tweets on Android from a Users Acc

answered 2011-07-24T16:14:17.917

Your Answer