简体   繁体   中英

Android Twitter4J retrieve OAuth and oauth_token_secret automatically?

I have implemented Twitter4J in my Android application and it's works fine. But after I login with twitter login page, it ask user to enter PIN code manually.

In Twitter4J class there is a method to retrieve and store(into sharedpreferences) the OAuth and oauth_token_secret.

/**
* Retrieve the oauth_verifier, and store the oauth and oauth_token_secret 
* for future API calls.
*/
@Override
protected Void doInBackground(Uri...params) {
        final Uri uri = params[0];
        final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);

        try {
            provider.retrieveAccessToken(consumer, oauth_verifier);

            final Editor edit = prefs.edit();
                    edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken());
                    edit.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret());
                    edit.commit();

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

                    consumer.setTokenWithSecret(token, secret);
                    context.startActivity(new Intent(context,MainAct.class));

                    executeAfterAccessTokenRetrieval();

                    Log.i(TAG, "OAuth - Access Token Retrieved");

                } catch (Exception e) {
                    Log.e(TAG, "OAuth - Access Token Retrieval Error", e);
                }

                return null;
    }

manifest - PrepareRequestTokenActivity

<activity android:name=".PrepareRequestTokenActivity" android:launchMode="singleTask">>
        <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="x-oauthflow-twitter" android:host="callback" />
        </intent-filter>
</activity>

I don't understand why it doesn't retrieve OAUTH_TOKEN and OAUTH_SECRET . How to authorize without entering PIN number? Am I doing something wrong?

Please help me.

Thank you

Hey this code might be helpful,

AccessToken accessToken = getAccessToken();

        Configuration conf = new ConfigurationBuilder()
                .setOAuthConsumerKey(TwitterConstants.CONSUMER_KEY)
                .setOAuthConsumerSecret(TwitterConstants.CONSUMER_SECRET)
                .setOAuthAccessToken(accessToken.getToken())
                .setOAuthAccessTokenSecret(accessToken.getTokenSecret())
                .build();

        OAuthAuthorization auth = new OAuthAuthorization(conf,
                conf.getOAuthConsumerKey(), conf.getOAuthConsumerSecret(),
                new AccessToken(conf.getOAuthAccessToken(),
                        conf.getOAuthAccessTokenSecret()));

and to get AccessToken,

public AccessToken getAccessToken() {
    String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
    String tokenSecret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

    if (token != null && tokenSecret != null)
        return new AccessToken(token, tokenSecret);
    else
        return null;
}

您可以尝试以下示例,而不是执行基于PIN的身份验证:https://github.com/ddewaele/AndroidTwitterSample

I solved the problem. It was a problem with the callback URL.

I edited constants like this and it worked.

public static final String  OAUTH_CALLBACK_SCHEME   = "x-oauthflow-twitter";
public static final String  OAUTH_CALLBACK_HOST     = "callback";
public static final String  OAUTH_CALLBACK_URL      = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;

thank you

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