How to authenticate with Google Drive API in Android emulator

I’m having trouble connecting to Google Drive from my Android app running on an emulator. I’ve set up everything according to the official documentation but keep getting authentication errors.

Here’s what I’ve configured so far:

Permissions in AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>

Activity configuration:

<activity
    android:name="com.myapp.MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <meta-data
        android:name="com.google.android.apps.drive.APP_ID"
        android:value="123456789-abcdefghijklmnop.apps.googleusercontent.com" />
</activity>

Java code for API client:

if (driveApiClient == null) {
    driveApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .setAccountName("[email protected]")
            .build();
}
driveApiClient.connect();

I keep getting INVALID_KEY errors in the logs. I’ve created both OAuth credentials and API keys in the Google Console, but the authentication still fails. Since I’m using an emulator, I’m not sure about the SHA1 fingerprint requirements. What am I missing here?

make sure your google-services.json file is in the app directory and matches your project setup. Also, try testing on a real device, since auth on emulators can be tricky. that INVALID_KEY msg usually indicates setup issues in the console.

I had the same authentication issues with Drive API on emulators. What tripped me up was that GoogleApiClient has been deprecated for ages. Google switched to GoogleSignIn and the newer Drive REST API v3 - they ditched the old Drive Android API. That INVALID_KEY error? It’s because you’re using outdated auth methods against services that expect the new flow. Switch to GoogleSignInOptions with Drive.SCOPE_FILE scope, then hit the Drive REST API directly with your authenticated account. Fixed all my auth problems and works great on emulators and devices. The old Drive.API just doesn’t work with current Google Play services.

The SHA1 fingerprint is likely the issue here. You’ll need to generate the debug keystore fingerprint specific to your development environment and ensure it’s listed in the Google Cloud Console. Use this command: keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android. Extract the SHA1 hash from the result and add it to your OAuth 2.0 client ID for Android. Also, verify that your package name matches exactly, as any discrepancies can lead to authentication errors. Remember that changes in the console may take a few minutes to take effect, so allow some time before retesting. If you’re new to this, consider using the Google Sign-In API instead of GoogleApiClient, as it’s much more straightforward.