0

I'm trying to implement the Drive API for Android to back-up a file to the AppFolder. I'm using the tutorial on Using the Google APIS Client Library for Java to integrate with Drive on Android and used some code snippets from the Quickstart.

  • When opening the activity with the drive API calls (like in this Activity from the Quickstart, the .connect() call is unsuccesful with error code SIGN_IN_REQUIRED, which seems logical to me.
  • I then proceed to login to my Google account. The login looks succesfull.
  • The activity gets resumed. As in the linked Activity file from the Quickstart, the onResume method calls .connect() again.
  • The .connect() call fails with the same error code (SIGN_IN_REQUIRED) and the process repeats.

In the API Manager of the Google Developer Console, I created a project for this app. In that project, I created an "OAuth 2.0 client ID" for the app with the debug package name and the SHA-1 for the debug key. In the manifest of the Quickstart, I didn't see the Client ID of said credential used anywhere.

Is this a known issue or did I make a mistake somewhere?

Edit:

I built the original Quickstart from scratch, and the error still occurred. It might therefore be an issue with the API Console rather than a client issue.

  • I've created a project for the app.
  • I've enabled the Drive API and Google+ API.
  • I've created an "API Key" credential with the package name of my application and SHA-1 of my debug signing key.
mDroidd
  • 1,143
  • 4
  • 16
  • 24
  • Have you override onConnectionFailed()? – Mani Jul 31 '17 at 09:05
  • Yes. It's practically the same as the tutorial, with a call to `connectionResult.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE);`. That call is reached (checked with breakpoint). – mDroidd Jul 31 '17 at 09:07
  • I've built the Quickstart without changing anything and the issue still occurs. It seems I'm doing something wrong in the API Console. OP edited. – mDroidd Jul 31 '17 at 15:15

4 Answers4

0

Not the solution I was expecting but here it is:

It turned out Android Studio was NOT signing my app with the debug key located at ~/.android/debug.keystore, but with a different key.

Without knowing which key it was, I was able to figure out its SHA-1 using the method described in this SO question.

mDroidd
  • 1,143
  • 4
  • 16
  • 24
0

This is because you didn't generate proper apk with proper SHA1 key or package name which need to be registered in the Google API console. The code from [Google android-quickstart] is 100% correct. The onResume method calls .connect() again is due to your SHA1 key or package name is not matched with the API console.

To solve the problem: (1) You should generate the Sha1 for your app first.. if you are using Android Studio and developing the app, please follow this to get the default sha1 of your app. [Remember don't use live sha1 key, otherwise you will get stuck on login!]
(2) Follow this to create your OAuth client ID by using default sha1. (3) Run [Google android-quickstart] again.

*Note: You do not have to copy any API client ID to your manifest or activity, because you are supposed to register your app with the specified package name and Sha1 Key in the API console successfully, so that Google can detect these things automatically when the app is started.

Lambert
  • 1
  • 1
0

As per reference of this project just remove below lines then onResume() was not call again and again

Try This:

@Override
protected void onPause() {
    if (mGoogleApiClient != null) {
        mGoogleApiClient.disconnect();
    }
    super.onPause();
}
Aanal
  • 1
0

That's right, exactly as mentioned above. When authenticating the GoogleDrive API, this API requests SHA1 from the application, and when we install the application in test mode on the emulator the authentication does not work, because SHA1 does not correspond to SHA1 of the keystore. To solve this problem we must configure our GRADLE in this way.

 signingConfigs{
    key{
        keyAlias 'your key alias'
        keyPassword 'your keypassword'
        storeFile file('keystore path')
        storePassword 'your storepassword'
    }
}
buildTypes {
    debug{
        signingConfig signingConfigs.key
    }
}


 buildTypes {
    release {
        shrinkResources true
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        debuggable false
        useProguard true
    }
    debug {
        shrinkResources true
        minifyEnabled true
        debuggable true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        useProguard true
    }
}

When you publish the application remove this part of the code

signingConfigs{
    key{
        keyAlias 'your key alias'
        keyPassword 'your keypassword'
        storeFile file('keystore path')
        storePassword 'your storepassword'
    }
}
buildTypes {
    debug{
        signingConfig signingConfigs.key
    }
}
AllanRibas
  • 339
  • 3
  • 10