Android Google 驱动器 API 身份验证

Android Google Drive API Authentication

我在 Android 应用程序上使用 Google Drive API 时遇到问题。在我的设备上,带有 Android 5.1.1 的 Nexus 4 运行良好。在我用户的设备上,我配置的同意屏幕没有显示,相反,只显示了一个包含用户电子邮件的列表选择对话框,但它没有执行任何操作。

我在 Android 5.0.1 的其他设备上看到了这个问题。

我使用生产 SHA1 密钥和同意屏幕配置了 OAuth 2.0 凭据。正如我所说,它在我的设备上运行良好。

我正在使用以下参数来编译我的应用程序:

还有这些 google 库:

谢谢!

这是我的代码现在的样子:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_RESOLUTION) {
        if (resultCode == RESULT_OK) {
            // Make sure the app is not already connected or attempting to connect.
            if (!mBackupAsyncTask.getGoogleApiClient().isConnecting() &&
                    !mBackupAsyncTask.getGoogleApiClient().isConnected()) {
                // Connect and execute the task.
                mBackupAsyncTask.getGoogleApiClient().connect();
            }
        }
    }
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(LOG_TAG, "GoogleApiClient connection failed: " + connectionResult.toString());

    if (!connectionResult.hasResolution()) {
        Log.i(LOG_TAG, "Error with no resolution.");
        // Show the localized error dialog.
        GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
    } else {
        // 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 {
            connectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (IntentSender.SendIntentException e) {
            Log.e(LOG_TAG, "Exception while starting resolution activity.", e);
        }
    }
}

public class DriveBackupAsyncTask extends ApiClientAsyncTask<Object, Void, Metadata> {

    public DriveBackupAsyncTask(Context context) {
        super(context);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressBar.setIndeterminate(true);
        mEfetuarBackupButton.setEnabled(false);
        mRestaurarBackupButton.setEnabled(false);
    }

    @Override
    protected Metadata doInBackgroundConnected(Object... params) {
        if (params[0].equals(TASK_BACKUP)) {
            DriveFile backupFile = DriveBackupHelper.createBackup(
                    getGoogleApiClient(),
                    APP_FOLDER,
                    BACKUP_FILE,
                    getDbFile());

            if (backupFile != null)
                return backupFile.getMetadata(getGoogleApiClient()).await().getMetadata();
        }

        if (params[0].equals(TASK_RESTAURAR)) {
            DriveFile backupFile = DriveBackupHelper.getBackupFile(
                    getGoogleApiClient(),
                    APP_FOLDER,
                    BACKUP_FILE);

            if (backupFile == null)
                return null;

            if (DriveBackupHelper.restoreBackup(getGoogleApiClient(), getDbFile(), backupFile))
                return backupFile.getMetadata(getGoogleApiClient()).await().getMetadata();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Metadata result) {
        super.onPostExecute(result);
        mProgressBar.setIndeterminate(false);
        mEfetuarBackupButton.setEnabled(true);
        mRestaurarBackupButton.setEnabled(true);

        if (result == null) {
            showError(getString(R.string.bkp_error));
        } else {
            showMessage(getString(R.string.bkp_success));
        }
    }
}

此说明假定您的代码基于 Google API Guide 用于处理连接失败,并且其结构类似于下面显示的示例(这是从指南中复制的):

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (mResolvingError) {
        // Already attempting to resolve an error.
        return;
    } else if (result.hasResolution()) {
        try {
            mResolvingError = true;
            // This statement causing posting of the account picker dialog
            // or other UI to resolve the connection problem.
            result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
        } catch (SendIntentException e) {
            // There was an error with the resolution intent. Try again.
            mGoogleApiClient.connect();
        }
    } else {
        // Show dialog using GooglePlayServicesUtil.getErrorDialog()
        showErrorDialog(result.getErrorCode());
        mResolvingError = true;
    }
}

您看到的对话框是在用户需要 select 帐户时创建的。它是由 onConnectionFailed():

中的语句生成的
result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);

其中 REQUEST_RESOLVE_ERROR 是您定义的整数常量。当用户 select 在对话框中创建帐户时,您的 activity 会收到请求代码等于 REQUEST_RESOLVE_ERROR 的结果。您的 activity 必须有一个方法来处理类似于此的结果(也从指南中复制):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_RESOLVE_ERROR) {
        mResolvingError = false;
        if (resultCode == RESULT_OK) {
            // Make sure the app is not already connected or attempting to connect
            if (!mGoogleApiClient.isConnecting() &&
                    !mGoogleApiClient.isConnected()) {
                mGoogleApiClient.connect();
            }
        }
    }
}

当您在 post 中说对话框 "doesn't do anything" 时,它表明您没有 onActivityResult() 方法,或者,如果有,代码没有处理对话框的结果正确。

如果这个答案对您没有帮助,而您已经有了一个 onActivityResult(),请 post 它连同 onConnectionFailed()