8

I connect a GoogleApiClient for use with Google Drive. I build the client like this:

        GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

My experience has been that the first time a connection request is made for this client, the AccountPicker dialog is presented and the consent screen for Google Drive. If the user selects an account, consents, and the connection completes successfully, the AccountManager or some related function saves the selected account as the default account, and the credentials (OAuth tokens?) for the Drive scope. On subsequent connection requests, as a convenience to the user, the saved values are used and the user does not see the UI for account selection or consent.

For development testing, I would like to be able to clear the default account and saved credentials so that I can exercise my connection failure resolution processing. I have not found a way to do this. I tried this without success:

String driveScope = "https://www.googleapis.com/auth/drive.file";
String tokenType = "oauth2:" + driveScope;

AccountManager.get(this).invalidateAuthToken(
    GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE, tokenType);
Bob Snyder
  • 37,759
  • 6
  • 111
  • 158

1 Answers1

10

It sounds like you should call clearDefaultAccountAndReconnect() on your GoogleApiClient to clear the selected account information.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 3
    Yes, that worked. Thanks. Sigh--so many classes, so many methods, so little time. – Bob Snyder Jun 30 '15 at 17:26
  • 1
    This is not a good solution. GoogleApiClient already has to be connected otherwise an exception is raised. There a lot of use cases when you want a user to pick an account before every use of GoogleApiClient ie. before every connect(). – f470071 Feb 13 '17 at 16:40
  • is there an only function to clear account? @ianhanniballake – SARATH V Jul 03 '18 at 08:21