Search code examples
google-drive-android-api

Not able to connect (in a project with variant)


Hi I'm trying to get some public drive files in our app and I can't manage to connect successfully to the api.

I've followed the tutorial, checked the details and also managed to make the Android Quickstart work(https://github.com/googledrive/android-quickstart) but I'm not connecting to the api.

API key created (several attempts):

enter image description here

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="org.demo.whatever"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission
        android:name="android.permission.GET_ACCOUNTS"
        tools:node="remove" />
    <uses-permission
        android:name="android.permission.USE_CREDENTIALS"
        tools:node="remove" />
    <uses-permission
        android:name="android.permission.READ_EXTERNAL_STORAGE"
        tools:node="remove" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        tools:node="remove" />

    <application
        android:name=".EyeSeeTeaApplication"
        android:allowBackup="true"
        android:icon="@drawable/qualityapp_logo"
        android:label="@string/app_name"
        android:theme="@style/EyeSeeTheme"
        tools:replace="android:icon,android:theme" >

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        ...

app/build.gradle:

productFlavors {
    eds {
        applicationId "org.demo.whatever.eds"
        versionName "EDS 2.0.0"
        minSdkVersion 15
        targetSdkVersion 21
    }
    hnqis {
        applicationId "org.demo.whatever.hnqispull"
        versionName "HNQIS 1.0.0"
        minSdkVersion 15
        targetSdkVersion 21
    }
}

Standard Activity code:

public class DashboardActivity extends BaseActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    ...

    @Override
    public void onResume(){
        Log.d(TAG, "onResume");
        super.onResume();
        getSurveysFromService();

        //FIXME
        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();
    }

                @Override
    public void onPause(){
        Log.d(TAG, "onPause");

        //FIXME
        if (mGoogleApiClient != null) {
            mGoogleApiClient.disconnect();
        }
        super.onPause();
    }

        @Override
    public void onConnected(Bundle bundle) {
        Log.i(TAG, "API client connected.");
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(TAG, "GoogleApiClient connection suspended");
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // Called whenever the API client fails to connect.
        Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
        if (!result.hasResolution()) {
            // show the localized error dialog.
            GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
            return;
        }
        // The failure has a resolution. Resolve it.
        // Called typically when the app is not yet authorized, and an
        // authorization
        // dialog is displayed to the user.
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (IntentSender.SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
        }
    }

        /**
     * Handles resolution callbacks.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        //FIXME Always resultCode ==0 instead of -1 (RESULT_OK)
        Log.d(TAG,String.format("onActivityResult(%d, %d)",requestCode,resultCode));
    }
    ...
}    

Question

I think there is something wrong with the package name considering that there are 2 flavours, but I've tried every reasonable combination and none is working. I've been downloaded a Package Name app to ensure I'm using the right package name in the API credential.

Any clue??


Solution

  • For future readers.

    1- You need to setup an OAuth api key in order to connect the client.

    2- In case your dealing with variants you must double check that the packageName is you applicationId (in case this is specified in app/build.gradle > productFlavors).