0

在我的应用程序中,我使用 oauth 登录到我的谷歌帐户,并且我正在检索所有用户帐户详细信息。问题是,由于我总是登录到我的主要 gmail 帐户,我的应用程序将其作为用户帐户。它从不提示输入用户登录名/密码。所以我无法尝试获取任何其他 gmail id 的帐户详细信息。

即使用户已经登录,我如何强制要求输入电子邮件和密码。我的请求令牌代码如下所示

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();
    }

}

}

4

2 回答 2

0

这里的问题不是您的代码,而是您的浏览器设置。

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);

您正在调用外部浏览器来打开 Google 的授权页面。如果用户已在该浏览器中登录 Google,则只会要求他批准您的申请。如果他还没有登录,他将首先看到登录屏幕。因此,只需在那里注销,您就可以以其他用户身份登录。

除了依赖外部浏览器,您还可以打开自己的WebView. 如果您自己不为 Web 视图实施 cookie 管理,则将始终要求用户在那里登录。

于 2012-10-03T17:26:54.177 回答
0

我可能会避免向最终用户询问密码,但是如果您想提示用户使用哪个电子邮件帐户(可能是已在相关设备上注册的帐户,也可能是新帐户),您可以开始帐户选择器意图。

示例伪代码:

//Applicable only to Google accounts for now.
        String[] accountTypes = new String[]{"com.google"};
        Intent intent = AccountPicker.newChooseAccountIntent(null, //There is no currently selected account.
                                                             null, //There is no restricted list of accounts.
                                                             accountTypes, //The dialog should list only accounts from the "com.google" domain.
                                                             true, //prompt the user to pick an account if there's only one available account (just use that one). Even if only one account currently exists, the dialog may include an option for the user to add a new account.
                                                             null, //There is no custom title for the dialog.
                                                             null, //There is no specific auth token type required.
                                                             null, //There are no restrictions based on account features.
                                                             null); //There are no authenticator-specific options.

        //Capture end-users account selection and proceed with SignIn. onActivityResult() will be called once the user has made their selection.
        startActivityForResult(intent, requestCode); //This will initiate a dialog.

一旦 startActivityForResult() 返回,即用户做出了选择,您可以通过以下方式检索选择的电子邮件地址:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case <the request code you passed to startActivityForResult:
                // Receiving a result from the AccountPicker
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        //Retrieve the chosen email account
                        data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME));
                    } else {
                        //Handle error
                    }
                } else if (resultCode == RESULT_CANCELED) {
                    // The account picker dialog closed without selecting an account.
                    // Notify users that they must pick an account to proceed.
                }
                break;
        }
    } 

请注意,最终用户不受使用 AccountManager 选择的帐户的限制。毕竟,一旦出现意图对话框,他们总是可以使用“添加帐户”选项。

希望这可以帮助。

干杯!

于 2014-12-25T06:20:49.260 回答