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.
This is the only way I can get the API to work from my SyncAdapter
- the new API refuses to work with blockingConnect()
:
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)
.build();
long start = System.currentTimeMillis();
mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
while (mGoogleApiClient.isConnecting() && System.currentTimeMillis() - start < 5000) {
try {
Thread.sleep(250, 0);
} catch (InterruptedException e) {
}
}
if (mGoogleApiClient.isConnected()) {
try {
// Do stuff with Google Drive.
} finally {
mGoogleApiClient.disconnect();
}
}