Google Plus GoogleApiClient connect () without account selection dialog

I am working on enabling Google+ login in the app and now it works for me mostly. Following the instructions here:

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

I was able to add a Sign In to Google button, integrate GoogleApiClient

from the Services Services, and allow the user to sign in to the Google account of their choice.

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

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

It's a simple call to call Plus.AccountApi.revokeAccessAndDisconnect()

with connected GoogleApiClient

. The problem is that the first thing to do is tie GoogleApiClient

.

The process of getting such an object is described in the first link: use GoogleApiClient.Builder

to create a client object, call connect()

on it and use the appropriate callbacks to detect connection success or failure. But if the user has multiple Google accounts on their phone, then it connect()

crashes immediately, requiring me to invoke a permission intent, which will pop up a dialog asking them to choose which account they want to use.

But it won't work for this case because they are already signed in to Google+, so I should already know which account to use. Remember, at this point, all I have to do is create an object GoogleApiClient

signed into the account they were logged into earlier, so I can pass it to Plus.AccountApi.revokeAccessAndDisconnect()

. But I don't know how to do it.

I went through the documentation and searched every combination of words I can think of to explain this problem, but I haven't found anything yet. Is there generally a way to create an instance GoogleApiClient

with a specific account rather than requiring the user to select from a list of existing accounts? Or, if not, is there any information stored in the Preferences that I can get to get the token of the account they have already allowed in my application?

+3


source to share


1 answer


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

Any curiosity, this is how you do it: you just need to add one more step to the GoogleApiClient builder; call the method setAccountName()

to specify which account you want to build the api client object to. So:

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

      



Becomes as follows:

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.

+7


source







All Articles