3
votes

I'm working on integrating Google+ sign-ins into an app, and I have it mostly working now. Following the instructions here:

https://developers.google.com/+/mobile/android/sign-in

I was able to add a "Sign In with Google" button, integrate the GoogleApiClient from Play Services, and allow the user to sign in to the Google account of their choice.

However, in order to comply with the Google terms of service, I also want to provide a way (in a different activity) for them to completely disconnect their Google+ account from the app. According to Google's documentation here:

https://developers.google.com/+/mobile/android/sign-in#revoking_access_tokens_and_disconnecting_the_app

This is a simple matter of calling Plus.AccountApi.revokeAccessAndDisconnect() with the connected GoogleApiClient. Problem is, that requires that I have a connected GoogleApiClient in the first place.

The process for getting such an object is outlined in the first link: Use GoogleApiClient.Builder to build the client object, call connect() on it, and use the appropriate callbacks to detect connection success or failure. But if the user has more than one Google account on their phone, then connect() leads to an immediate failure requiring me to call the resolution intent which will pop up a dialog asking them to pick which account they want to use.

But that won't work for this case, because they've already signed in to Google+, so I should already know which account to use. Remember, at this point, all I need to do is create a GoogleApiClient object signed in to the account they logged in to previously, so I can then pass it to Plus.AccountApi.revokeAccessAndDisconnect(). But I have no idea how to do that.

I've looked through the documentation and searched every combination of words I can think of to explain this issue, but so far I have found nothing. Is there any way at all to instantiate GoogleApiClient with a specific account instead of requiring the user to pick from the list of existing accounts? Or, if not, is there some information saved to Preferences that I can access to get a token to the account they have already authorized in my app?

1

1 Answers

8
votes

Of course, as soon as I post this I stumble across the answer.

For anyone curious, here's how you do it: you just need to add one more step to the GoogleApiClient builder; call the setAccountName() method to specify which account you want to build the api client object for. So this:

mGoogleApiClient = new GoogleApiClient.Builder(this)
                       .addConnectionCallbacks(this)
                       .addOnConnectionFailedListener(this)
                       .addApi(Plus.API)
                       .addScope(Plus.SCOPE_PLUS_PROFILE)
                       .build();

Becomes this:

mGoogleApiClient = new GoogleApiClient.Builder(this)
                       .addConnectionCallbacks(this)
                       .addOnConnectionFailedListener(this)
                       .addApi(Plus.API)
                       .addScope(Plus.SCOPE_PLUS_PROFILE)
                       .setAccountName(<gmail address>)
                       .build();

And then it works.