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

拜拜、爱过 提交于 2019-11-30 06:56:18
Blundell

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

The reason is that after the authentication returns, you created another instance of the activity and used rToken in its onResume method, which is null.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!