无法连接(在具有变体的项目中)

Not able to connect (in a project with variant)

您好,我正在尝试在我们的应用程序中获取一些 public 驱动器文件,但我无法成功连接到 api。

我已经按照教程进行了操作,检查了详细信息,还设法让 Android 快速入门工作(https://github.com/googledrive/android-quickstart),但我 没有连接到 api.

API 已创建密钥(多次尝试):

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
    }
}

标准Activity代码:

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));
    }
    ...
}    

问题

考虑到有 2 种口味,我认为包名称有问题,但我已经尝试了所有合理的组合并且 none 正在工作。我已经下载了一个包名称应用程序,以确保我在 API 凭证中使用正确的包名称。

有线索吗??

.

  1. APIs & auth -> Credentials -> Credentials -> Android client 1 'SHA1' MUST BE IN SYNC with your 'Pakage name'
  2. APIs & auth -> Credentials -> OAuth consent screen MUST have your 'Product Name' and 'Email address'

如果您使用正确的 SHA1 进行授权,您可能还需要检查应用的 keystore

对于未来的读者。

1- 您需要设置 OAuth api 密钥才能连接 客户端。

2- 如果您处理变体,您必须仔细检查 packageName 是您 applicationId(如果这是在app/build.gradle > productFlavors).