1

I am trying to integrate the Google Drive API for Android with my Android application.

My senior created a project on the Google Developer console using the app package name and the SHA1 from my machine and generated the OAuth 2.0 client ID (I have no access to the account).

On the click of a button, I have to enable the user to access the files on Drive so as to upload them to the server.

I call the following code onClick():

mGoogleApiClient = new GoogleApiClient.Builder(Activity.this)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .addConnectionCallbacks(Activity.this)
    .addOnConnectionFailedListener(Activity.this)
    .build();

mGoogleApiClient.connect();

This calls the onConnectionFailed() callback:

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "Connection failed");
    if (connectionResult.hasResolution()) {
        try {
            Log.i(TAG, "Connection failed - Trying again");
            connectionResult.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE);
        } catch (IntentSender.SendIntentException e) {
            // Unable to resolve, message user appropriately
        }
    } else {
        Log.i(TAG, "Connection failed, Code : " + connectionResult.getErrorCode());
        GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
    }
}

The connection fails and it tries again. On retrying, it calls the onActivityResult() and tries to connect again:

public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (requestCode == Constants.RESOLVE_CONNECTION_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        Log.i(TAG, "Code match");
        mGoogleApiClient.connect();
    }
}

This time again the connection attempt fails, with error code 8 and gives the message "App is having trouble with Google Play Services. If the problem persists, please contact the developer for assistance."

I read this too: <Your App> is having trouble with Google Play services - When using GoogleApiClient, it always trigger onConnectionFailed, but did not help.

I tried looking it up on Google but nothing has helped yet. Any assistance will be great!

Community
  • 1
  • 1
Bot
  • 622
  • 2
  • 10
  • 21

1 Answers1

0

Try not to instatiate mGoogleApiClient in your click onClick() function. If you check this android-demo and this drive quickstart, the mGoogleApiClient is instantiated in the onResume() lifecycyle:

@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
// Create the API client and bind it to an instance variable.
// We use this instance as the callback for connection and connection
// failures.
// Since no account name is passed, the user is prompted to choose.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// Connect the client. Once connected, the camera is launched.
mGoogleApiClient.connect();
}

Check the official Android Drive API for more info.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • Yes. I have checked that. But that would cause the Account selection dialog to pop up `onResume()`. I do not want that. I want that dialog to be shown only in case the user wants to access files from Google Drive. – Bot Oct 06 '16 at 09:32