Search code examples
javaandroidtwitter4j

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


Solution

  • 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