Search code examples
androidgoogle-api-clientgoogle-drive-android-api

Google Drive API Android account selector always returns RESULT_CANCELED for some accounts


I am integrating Google Drive API into my Android app to store a JSON file in the App Storage folder.

To do this I've implemented the Google Drive API in a fragment inside my MainActivity.

When I execute this code, it hits the onConnectionFailed method with the SIGN_IN_REQUIRED code, as expected. I execute startResolutionForResult and the account picker appears.

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "GoogleApiClient connection failed: " + connectionResult.toString());
    if (connectionResult.hasResolution()) {
        if(!mConnectionResolutionInProgress)
        {
            try {
                mConnectionResolutionInProgress = true;
                connectionResult.startResolutionForResult(getActivity(), REQUEST_CODE_RESOLUTION);
            } catch (IntentSender.SendIntentException e) {
                // Unable to resolve, message user appropriately
                showMessage("There was an issue connecting to Google Drive services.");                    
            }
        }
        else
        {                
            mConnectionResolutionInProgress = false;
            showMessage("Canceling export/import action");                
        }
    } 
    else 
    {
        mConnectionResolutionInProgress = false;
        GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), getActivity(), 0).show();            
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_CODE_RESOLUTION)
    {
        mConnectionResolutionInProgress = false;
        if(resultCode == Activity.RESULT_OK)
        {
            if(!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting())
            {
                ConnectToGoogleDrive();
            }
        }
    }
}

private void ConnectToGoogleDrive()
{
    if(mGoogleApiClient == null)
    {
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    mGoogleApiClient.connect();
}

The strange thing is with my personal account (and my work account) it works just fine. I click my account, then dialog disappears, and it's replaced with the permissions dialog. If I accept that then the operation continues perfectly. The onActivityResult returns resultCode RESULT_OK.

If I use anyone else's account, the account picker disappears and I hit one of my error cases. If I debug I see that the resultCode is actual RESULT_CANCELED.

I don't see what the difference is. It looks like my code is pretty standard.

Any ideas?


Solution

  • It could be an app signature issue - if you are testing successfully on debug, but failing on release, you may not have the correct keystore signatures set up in the Google API Console - if that is the case, you should be able to add the second signature to the console as well, and both debug and release builds will work fine.