0

I'm trying to figure out Twitter OAuth for my Android application. My problem is when the Twitter OAuth browser opens, I click the Authorize App button but it directs to my callback URL, pauses there and doesn't go back to my app, subsequently not calling either of the onNewIntent/onResume methods (I have tried to use both of these two methods and neither are being called). My activity code and manifest XML file are below. Really appreciate for any help or hint. Thank you.

public class TwitterLoginActivity extends SherlockPreferenceActivity {

private static final String TAG = "TwitterDemo";

private static String CONSUMER_KEY = ""; // filled with my consumer key
private static String CONSUMER_SECRET = ""; //filled with my consumer secret
private static final String CALLBACK_SCHEME = "http";
private String CALLBACK_URL= CALLBACK_SCHEME+"://mjelajahprd.appspot.com/twitter/index";

private OAuthSignpostClient oauthClient;
private OAuthConsumer mConsumer;
private OAuthProvider mProvider;
private Twitter twitter;
private SharedPreferences prefs;
private String authUrl;
private WebView view;
private Boolean status;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        mConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
                CONSUMER_SECRET);

        mProvider = new DefaultOAuthProvider(
                "http://api.twitter.com/oauth/request_token",
                "http://api.twitter.com/oauth/access_token",
                "http://api.twitter.com/oauth/authorize");

        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String token = prefs.getString("token", null);
        String tokenSecret = prefs.getString("tokenSecret", null);

        if (token != null && tokenSecret != null) {
            mConsumer.setTokenWithSecret(token, tokenSecret);
            oauthClient = new OAuthSignpostClient(CONSUMER_KEY,
                    CONSUMER_SECRET, token, tokenSecret);
        } else {
            Log.d(TAG, "onCreate. Not Authenticated Yet ");
            new OauthAuthorizedTask().execute();


        }

    } catch (Exception e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

private class OauthAuthorizedTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        //String authUrl;
        String message = null;
        Log.d(TAG, "OAuthAuthorizeTask mConsumer: " + mConsumer);
        Log.d(TAG, "OAuthAuthorizeTask mProvider: " + mProvider);
        try {
            authUrl = mProvider.retrieveRequestToken(mConsumer,
                    CALLBACK_URL);
            Intent oauthIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(authUrl));
            oauthIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            oauthIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            oauthIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
            startActivity(oauthIntent);


        } catch (OAuthMessageSignerException e) {
            message = "OAuthMessageSignerException";
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            message = "OAuthNotAuthorizedException";
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            message = "OAuthExpectationFailedException";
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            message = "OAuthCommunicationException";
            e.printStackTrace();
        }
        return message;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (result != null) {
            Toast.makeText(TwitterLoginActivity.this, result,
                    Toast.LENGTH_LONG).show();
        }
    }
}


@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.d(TAG, "intent: " + intent);

    // Check if this is a callback from OAuth
    Uri uri = intent.getData();
    if (uri != null && uri.getScheme().equals(CALLBACK_SCHEME)) {
        Log.d(TAG, "callback: " + uri.getPath());

        String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
        Log.d(TAG, "verifier: " + verifier);
        Log.d(TAG, " xxxxxxxxxxx mConsumer access token: " + mConsumer.getToken());
        Log.d(TAG, " xxxxxxxxxxxx mConsumer access token secret: " + mConsumer.getTokenSecret());
        Log.d(TAG, " xxxxxxxxxxxxx OAuth.OAUTH_TOKEN: " + OAuth.OAUTH_TOKEN);
        Log.d(TAG, " xxxxxxxxxxxxx OAuth.OAUTH_TOKEN_SECRET: " + OAuth.OAUTH_TOKEN_SECRET);

        new RetrieveAccessTokenTask().execute(verifier);
    }
}


class RetrieveAccessTokenTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String message = null;
        String oauthVerifier = params[0];
        try {
            // Get the token
            Log.d(TAG, " RetrieveAccessTokenTask mConsumer: " + mConsumer);
            Log.d(TAG, " RetrieveAccessTokenTask mProvider: " + mProvider);
            Log.d(TAG, " RetrieveAccessTokenTask verifier: " + oauthVerifier);
            mProvider.retrieveAccessToken(mConsumer, oauthVerifier);
            String token = mConsumer.getToken();
            String tokenSecret = mConsumer.getTokenSecret();
            mConsumer.setTokenWithSecret(token, tokenSecret);

            Log.d(TAG, String.format(
                    "verifier: %s, token: %s, tokenSecret: %s", oauthVerifier,
                    token, tokenSecret));

            // Store token in prefs
            prefs.edit().putString("token", token)
                    .putString("tokenSecret", tokenSecret).commit();

            // Make a Twitter object
            oauthClient = new OAuthSignpostClient(CONSUMER_KEY,
                    CONSUMER_SECRET, token, tokenSecret);
            twitter = new Twitter(null, oauthClient);

            Intent mainIntent = new Intent(TwitterLoginActivity.this, MainActivity.class);
            startActivity(mainIntent);

            Log.d(TAG, "token: " + token);
        } catch (OAuthMessageSignerException e) {
            message = "OAuthMessageSignerException";
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            message = "OAuthNotAuthorizedException";
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            message = "OAuthExpectationFailedException";
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            message = "OAuthCommunicationException";
            e.printStackTrace();
        }
        return message;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (result != null) {
            Toast.makeText(TwitterLoginActivity.this, result,
                    Toast.LENGTH_LONG).show();
        }
    }
}

}

Manifest.xml (note: already set uses-permission android:name="android.permission.INTERNET")

    <activity
        android:name=".TwitterLoginActivity"
        android:label="@string/twitter"
        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:host="mjelajah-prd.appspot.com/twitter/index"
                android:scheme="http" >
            </data>
        </intent-filter>
    </activity>
richsage
  • 26,912
  • 8
  • 58
  • 65
vie
  • 11
  • 3

1 Answers1

2

Try with android:launchMode="singleInstance" .

private final String CALLBACKURL = "sosInternational:///Test";

<intent-filter>  
    <action android:name="android.intent.action.VIEW"></action>  
    <category android:name="android.intent.category.DEFAULT"></category>  
    <category android:name="android.intent.category.BROWSABLE"></category>  
    <data android:scheme="sosInternational" android:host="TwitterLoginActivity"></data>  
</intent-filter>

Look at this link for more details .

http://www.android10.org/index.php/articleslibraries/291-twitter-integration-in-your-android-application

Problem in Callback in Twitter in Android

Community
  • 1
  • 1
Chirag
  • 56,621
  • 29
  • 151
  • 198
  • Thank you for your response, I have tried as per your suggestion above and still pause in my callback url. – vie Oct 05 '12 at 06:28
  • still not working and got same issue,it pause in my callback URL..I have duplicated all code at link you provided. Btw I cannot find Twitter Registration Page that mention in that link. I just create application in twitter now and provide callback URL same as I use in my activity and manifest code. Where I can find the Twitter Registration Page anyway? when I click the link it just direct to my email subscriptions page. – vie Oct 05 '12 at 07:53
  • Finally...got the solutions!! thanks for @w3m for postthe solution here http://stackoverflow.com/questions/6813996/problem-in-callback-in-twitter-in-android – vie Oct 05 '12 at 09:00