我已经实现了一个代码,通过opener活动在Google Drive Android应用程序上打开Google Drive中选定的文件。在此应用程序中,我需要放置注销选项以选择不同的帐户。但此代码将只要求登录一次,之后我无法从应用程序中删除该已登录用户。
是否可以使用Google Drive Android Api从Google帐户注销并再次显示帐户选择器?
BaseDemoActivity。Java语言
public abstract class BaseDemoActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "BaseDriveActivity";
/**
* DriveId of an existing folder to be used as a parent folder in
* folder operations samples.
*/
public static final String EXISTING_FOLDER_ID = "folderid";
/**
* DriveId of an existing file to be used in file operation samples..
*/
public static final String EXISTING_FILE_ID = "fileid";
/**
* Extra for account name.
*/
protected static final String EXTRA_ACCOUNT_NAME = "account_name";
/**
* Request code for auto Google Play Services error resolution.
*/
protected static final int REQUEST_CODE_RESOLUTION = 1;
/**
* Next available request code.
*/
protected static final int NEXT_AVAILABLE_REQUEST_CODE = 2;
/**
* Google API client.
*/
private GoogleApiClient mGoogleApiClient;
/**
* Called when activity gets visible. A connection to Drive services need to
* be initiated as soon as the activity is visible. Registers
* {@code ConnectionCallbacks} and {@code OnConnectionFailedListener} on the
* activities itself.
*/
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}
/**
* Handles resolution callbacks.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
mGoogleApiClient.connect();
}
}
/**
* Called when activity gets invisible. Connection to Drive service needs to
* be disconnected as soon as an activity is invisible.
*/
@Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
/**
* Called when {@code mGoogleApiClient} is connected.
*/
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "GoogleApiClient connected");
}
/**
* Called when {@code mGoogleApiClient} is disconnected.
*/
@Override
public void onConnectionSuspended(int cause) {
Log.i(TAG, "GoogleApiClient connection suspended");
}
/**
* Called when {@code mGoogleApiClient} is trying to connect but failed.
* Handle {@code result.getResolution()} if there is a resolution is
* available.
*/
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
// show the localized error dialog.
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
return;
}
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
/**
* Shows a toast message.
*/
public void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
/**
* Getter for the {@code GoogleApiClient}.
*/
public GoogleApiClient getGoogleApiClient() {
return mGoogleApiClient;
}
}
主要活动。Java语言
public class MainActivity extends BaseDemoActivity {
private static final String TAG = "MainActivity";
private static final int RC_SIGN_IN = 0;
private boolean mIntentInProgress;
private ConnectionResult mConnectionResult;
private static final int REQUEST_CODE_OPENER = 1;
private boolean browsefile = false;
private boolean mSignInClicked;
private String FILE_ID;
TextView filepath;
Button choosefilebutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
choosefilebutton = (Button) findViewById(R.id.choosefilebutton);
filepath = (TextView) findViewById(R.id.filepath);
choosefilebutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
open();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_logout:
signOutFromGplus();
choosefilebutton.setVisibility(View.GONE);
break;
default:
break;
}
return true;
}
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
//mSignInClicked = false;
}
private void open(){
IntentSender intentSender = Drive.DriveApi
.newOpenFileActivityBuilder()
.build(getGoogleApiClient());
try {
browsefile = true;
startIntentSenderForResult(
intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0);
} catch (SendIntentException e) {
Log.w(TAG, "Unable to send intent", e);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_OPENER:
if (resultCode == RESULT_OK && browsefile == true) {
showMessage("Result ok...");
if (!getGoogleApiClient().isConnecting()) {
getGoogleApiClient().connect();
}
DriveId driveId = (DriveId) data.getParcelableExtra(
OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
showMessage("Selected file's ID: " + driveId.getResourceId());
FILE_ID = driveId.getResourceId();
if(FILE_ID != null){
String link = "https://docs.google.com/file/d/"+FILE_ID;
filepath.setText(link);
Uri path = Uri.parse(link);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(path);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (Exception e) {
showMessage(e.getMessage());
}
}
}
//finish();
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
/**
* Sign-out from google
* */
private void signOutFromGplus() {
if (getGoogleApiClient().isConnected()) {
getGoogleApiClient().disconnect();
showMessage("Sign out Success...");
}
}
}
看看我在SO 21610239中的回答,特别是更新2014-11-04部分。
如何退出Google Drive服务?我不想切换帐户,而是明确退出。 我正在开发一个应用程序,它将使用用户的Google Drive存储和其他存储服务。因此,我需要允许用户注销Google Drive。
我们的应用程序正在使用 gapi.auth.登录进行身份验证。问题是,当用户登录到多个帐户时,不会显示帐户选择下拉列表。目前,为了克服这个问题,应用程序设置。显然,这并不是很有效。 使用时是否可以显示多用户选择下拉列表gapi.auth.signin? 用gapi.auth.authorize代替吗?(相关问题) 非常感谢。
我的应用程序中有以下工作流: 用户登录我的自定义应用程序 用户点击一个按钮链接他们的YouTube帐户 应用程序使用下面的代码列表发出服务器端请求 用户被重定向到google auth url 此时,发生了两件事之一: [我从来不想要这种行为] - 如果用户只登录了一个谷歌帐户(即mail,谷歌域名应用套件等),则永远不会要求用户选择要链接的帐户。它只是假设他们想要使用他们登录的那个,并继续其快乐
我正在使用谷歌Picker Api访问谷歌驱动器文件,工作正常。 但在我的浏览器中,如果我已经登录到多个Gmail帐户,它会自动显示一个帐户的驱动器文件。如果我已经登录了多个Gmail帐户,是否有办法显示帐户选择页面。 任何人都可以帮我。
在我的 Web 应用程序中,我想实现以程 用户点击使用Google按钮登录 用户在重定向屏幕中选择帐户 我收到一个JWT,其中包含帐户ID和电子邮件地址 我将帐户ID设置为登录提示,并仅设置“同意”提示 我创建Auth URL并再次重定向到Google以获得同意 在下一个同意屏幕中,用户应该只需要批准同意,而不是第二次选择他的帐户 我处理来自同意的回调 根据文档,我实现了“使用Google按钮登录
所以我已经在我的React应用中使用AWS Amplify (federatedSignIn)实现了Google认证。显然,我想让谷歌帐户选择器屏幕强制用户选择一个帐户登录。显然,它只有在有多个谷歌账户可供选择时才起作用,但如果只有一个账户,系统会自动使用它登录? 如何在只有一个帐户的情况下强制执行选择屏幕?