Dave Cole Dave Cole - 3 months ago 61
Android Question

GoogleApiClient.blockingConnect() does not work with GOOGLE_SIGN_IN_API

I have a

SyncAdapter
(code does not run on the UI thread) that writes to the app folder on Google Drive. This was working fine with the
GoogleApiClient
without Google Sign-In, but I am trying to move to
GoogleApiClient
with Google Sign-In.

The working code without Google Sign-In:

mGoogleApiClient = new GoogleApiClient.Builder(context)
.setAccountName(accountName)
.addApi(Drive.API)
.addScope(Drive.SCOPE_APPFOLDER)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.blockingConnect();


Now that I have moved to using Google Sign-In it no longer works. The following code is what I am trying to use:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
.build();
mGoogleApiClient = new GoogleApiClient.Builder(mApplication)
.addApi(Drive.API)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.blockingConnect();


My program falls over with the following error on the call to
blockingConnect()
:

java.lang.IllegalStateException: Cannot use SIGN_IN_MODE_REQUIRED with GOOGLE_SIGN_IN_API. Use connect(SIGN_IN_MODE_OPTIONAL) instead.


Is there a way to perform a
blockingConnect()
with the Google Sign-In API?

Note that I perform an initial Sign-In on the UI thread before the
SyncAdapter
attempts the above code.

Answer

It turns out that my code was incorrect and you should not call the connect() or blockingConnect() method at all with the new Google Sign-In API.

When I change my code to look like the following it works:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
        .build();
mGoogleApiClient = new GoogleApiClient.Builder(mApplication)
        .addApi(Drive.API)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
GoogleSignInResult result = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient).await();