简体   繁体   中英

Invalid Access Token format Twitter4j 2.2.6

This is my stack trace, and I can't figure out whats wrong!

08-30 12:26:50.494: E/AndroidRuntime(19465): FATAL EXCEPTION: main
08-30 12:26:50.494: E/AndroidRuntime(19465): java.lang.IllegalArgumentException: Invalid access token format.
08-30 12:26:50.494: E/AndroidRuntime(19465):    at twitter4j.auth.AccessToken.<init>(AccessToken.java:50)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at com.project.p.twitter.TwitterUtils.isAuthenticated(TwitterUtils.java:21)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at com.project.p.activities.Activity$4.onClick(Activity.java:313)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at android.view.View.performClick(View.java:3591)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at android.view.View$PerformClick.run(View.java:14263)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at android.os.Handler.handleCallback(Handler.java:605)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at android.os.Looper.loop(Looper.java:137)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at android.app.ActivityThread.main(ActivityThread.java:4507)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at java.lang.reflect.Method.invokeNative(Native Method)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at java.lang.reflect.Method.invoke(Method.java:511)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-30 12:26:50.494: E/AndroidRuntime(19465):    at dalvik.system.NativeStart.main(Native Method)

It's pointing me to this code

public static boolean isAuthenticated(SharedPreferences prefs) {

    String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
    String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

    AccessToken a = new AccessToken(token, secret);

    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(Constants.CONSUMER_KEY,
            Constants.CONSUMER_SECRET);
    twitter.setOAuthAccessToken(a);

    try {
        twitter.getAccountSettings();
        return true;
    } catch (TwitterException e) {
        return false;
    }
}

specifically the line

AccessToken a = new AccessToken(token, secret);

It was all working fine when I used the twitter4j 2.1.1 but I had to update for other reasons

Instead of taking OAuth.OAUTH_TOKEN and OAuth.OAUTH_TOKEN_SECRET from prefs, go to your application page in Twitter and create your access token which you can do with a button on the bottom of the Details tab and it will give you the parameters you need.

Use those instead of token and secret strings in your code.

I do not know if this is the healthiest way to solve this problem since we are hard coding these values, but it solved my problem for the time being.

Change

twitter.getAccountSettings();

to

twitter.verifyCredentials();

Worked for me..


[Edit]

Also capture the exception when creating the AccessToken with empty token and secret.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM