在我的应用程序中,我使用 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();
}
}
}