Search code examples
androidoauthgmail

android : login gmail with oauth is not asking for user id or password


In my app i am loggint in to my google account using oauth and i am retrieving all the user account details. The issue is that since i am always logged on to my primary gmail account , my app is taking that as the user account. It never prompts for user login / password. So i am not able to try getting the account details of any other gmail id.

how can i force to ask for email and password even if the user is already logged in. My request token code is given below

public class RequestTokenActivity extends Activity {


ProgressDialog progressDialog;
private OAuthConsumer consumer; 
private OAuthProvider provider;
private SharedPreferences prefs;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("create","create");

    try {
        consumer = new CommonsHttpOAuthConsumer(C.CONSUMER_KEY, C.CONSUMER_SECRET);
        provider = new CommonsHttpOAuthProvider(
                C.REQUEST_URL  + "?scope=" + URLEncoder.encode(C.SCOPE, C.ENCODING) + "&xoauth_displayname=" + C.APP_NAME,
                C.ACCESS_URL,
                C.AUTHORIZE_URL);
    } catch (Exception e) {
        Log.e(C.TAG, "Error creating consumer / provider",e);
    }

    //getRequestToken();
    new OAuthRequestTokenTask(this,consumer,provider).execute();
}


@Override
public void onNewIntent(Intent intent) {
    Log.i("newin","newintent");
    super.onNewIntent(intent); 
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    final Uri uri = intent.getData();
    if (uri != null && uri.getScheme().equals(C.OAUTH_CALLBACK_SCHEME)) {
        Log.i(C.TAG, "Callback received : " + uri);
        Log.i(C.TAG, "Retrieving Access Token");
        getAccessToken(uri);
    }
    else {
        new OAuthRequestTokenTask(this,consumer,provider).execute();
    }
}

private void getRequestToken() {
    try {
        Log.d(C.TAG, "getRequestToken() called");
        String url = provider.retrieveRequestToken(consumer, C.OAUTH_CALLBACK_URL);
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
        this.startActivity(intent);

    } catch (Exception e) {
        Log.e(C.TAG, "Error retrieving request token", e);
    }
}

private void getAccessToken(Uri uri) {
    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);
        this.startActivity(new Intent(this ,OAuthMain.class));

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

    } catch (Exception e) {
        Log.e(C.TAG, "Access Token Retrieval Error", e);
    }
}
public class OAuthRequestTokenTask extends AsyncTask<Void, Void, Void> {

    final String TAG = getClass().getName();
    private Context context;
    private OAuthProvider provider;
    private OAuthConsumer consumer;

    /**
     * 
     * We pass the OAuth consumer and provider.
     * 
     * @param   context
     *          Required to be able to start the intent to launch the browser.
     * @param   provider
     *          The OAuthProvider object
     * @param   consumer
     *          The OAuthConsumer object
     */
    public OAuthRequestTokenTask(Context context,OAuthConsumer consumer,OAuthProvider provider) {
        this.context = context;
        this.consumer = consumer;
        this.provider = provider;
    }
 @Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
     progressDialog = new ProgressDialog(RequestTokenActivity.this);
    progressDialog.setCancelable(true);
    progressDialog.setMessage(" Loading ...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setProgress(0);
    progressDialog.setOnCancelListener(new OnCancelListener() {

        public void onCancel(DialogInterface arg0) {
            if (progressDialog.isShowing()) 
                progressDialog.dismiss();
            finish();

        }
    });

    progressDialog.show();

}
    /**
     * 
     * Retrieve the OAuth Request Token and present a browser to the user to authorize the token.
     * 
     */
    @Override
    protected Void doInBackground(Void... params) {

        try {
            Log.i(TAG, "Retrieving request token from Google servers");
            final String url = provider.retrieveRequestToken(consumer, C.OAUTH_CALLBACK_URL);
            Log.i(TAG, "Popping a browser with the authorize URL : " + url);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
            context.startActivity(intent);

        } catch (Exception e) {
            Log.e(TAG, "Error during OAUth retrieve request token", e);
        }

        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        progressDialog.dismiss();
    }

}

}


Solution

  • The problem here is not your code, but your browser settings.

    With

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(
        Intent.FLAG_ACTIVITY_SINGLE_TOP
        | Intent.FLAG_ACTIVITY_NO_HISTORY
        | Intent.FLAG_FROM_BACKGROUND);
    context.startActivity(intent);
    

    you are calling an external browser to open Google's authorization page. If the user is already logged in to Google in that browser, he will just be asked to approve your application. If he was not logged in already, he will first be presented with the login screen. So just log out there and you can log in as another user.

    Instead of relying on an external browser, you could also open your own WebView. If you don't implement cookie management yourself for the web view, the user will always be asked to login there.