提问者:小点点

Android谷歌驱动器API身份验证


我在我的Android应用程序上遇到了谷歌云端硬盘API的问题。在我的设备上,装有Android5.1.1的Nexus 4工作正常。在我的用户设备上,我配置的同意屏幕没有显示,相反,只是显示了一个带有用户电子邮件的列表选择对话框,但它什么也没做。

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

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

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

  • min Sdk版本=15
  • t arge SdK Ve rion=22
  • 编译dk版本=22
  • 构建工具:22.0.1
  • 平台工具:23rc3

还有这些谷歌库:

  • com.android.support: support-v 4:22.2.0
  • com.android.support: app comp at-v 7:22.2.0
  • com.android.support:设计: 22.2.0
  • com.google.android. gm s:play-services-ads:7.5.0
  • com.google.android. gm s:播放-服务-驱动:7.5.0

谢谢你!

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

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

共1个答案

匿名用户

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

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

您看到的对话框是在用户需要选择帐户时创建的。它是由onConnectionFail()中的此语句生成的:

result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);

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

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

当您在帖子中说对话框“不做任何事情”时,它表明您没有onActivityResult()方法,或者,如果您有,则那里的代码没有正确处理对话框的结果。

如果此答案对您没有帮助并且您已经有一个onActivityResult(),请将其与onConnectionFail()一起发布。