How to sign out of a Google Drive account?

2.2k views Asked by At

How do I log out of the Google Drive service? I do not want to switch accounts but to log out definitively.

I am working on an app that will use the user's Google Drive storage among other storage services. Therefore I need to allow the user to sign out of Google Drive.

2

There are 2 answers

2
Xavi Gil On

Leaving aside I think this is a poor API implementation, this is how you log out.

mGoogleApiClient.unregisterConnectionCallbacks(mConnectionCallbacksListener);
mGoogleApiClient.unregisterConnectionFailedListener(mOnConnectionFailedListener);
mGoogleApiClient.clearDefaultAccountAndReconnect();

The unregistration of the callbacks is to avoid the API showing the Google Account picker, in other words, to avoid reconnecting. Remember to set the callbacks again if you are going to sign in to any Google service later!

Optionally, you can get the operation's result:

mGoogleApiClient.clearDefaultAccountAndReconnect().setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(Status status) {
            // Optional
        }
    });
0
mswlogo On

I found that this works. These are the same sign in options I use. Replace with your own.

I put this right before my sign in code so the user chooses an account each time.

    val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestScopes(Drive.SCOPE_FILE).requestScopes(Drive.SCOPE_APPFOLDER).build()
    val googleSignInClient = GoogleSignIn.getClient(this, signInOptions)
    googleSignInClient.signOut()