Android API Google Drive“连接失败”

时间:2014-11-20 14:57:38

标签: android google-drive-api

我尝试在Android上使用API​​ Google Drive,首先使用演示:

https://github.com/googledrive/android-quickstart

但是,我有这个错误,我无法解决。

  

GoogleApiClient连接失败:   ConnectionResult {的StatusCode = SIGN_IN_REQUIRED,   resolution = PendingIntent {421d40e8:android.os.BinderProxy@42137f78}}

@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.
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 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 (SendIntentException e) {
        Log.e(TAG, "Exception while starting resolution activity", e);
        // There was an error with the resolution intent. Try again.
        mGoogleApiClient.connect();
    }
}


@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    if (requestCode == REQUEST_CODE_RESOLUTION) {

        if (resultCode == RESULT_OK) {
            Log.i(TAG, "Error resolution success.");

            // Make sure the app is not already connected or attempting to connect
            if (!mGoogleApiClient.isConnecting() &&
                    !mGoogleApiClient.isConnected()) {
                mGoogleApiClient.connect();
            }

        } else {
            GooglePlayServicesUtil.getErrorDialog(requestCode, this, 0).show();
        }

        break;
    }
}

4 个答案:

答案 0 :(得分:3)

这可能是由于开发人员的疏忽(因为它发生在我身上) - 您在控制台上提供的SHA1详细信息是生产密钥的详细信息,您正在测试调试模式(SHA1详细信息会有所不同)。但是,谷歌驱动器API的错误消息可能会更好!

答案 1 :(得分:2)

Here是您收到的错误的信息。我假设您是,但您需要登录才能访问云端硬盘。当我运行示例应用程序时,它一开始就要求我选择一个帐户。也许您没有与您使用的设备同步的帐户?可以选择"添加帐户",但是当您拥有零帐户时,行为可能会有所不同。文档建议您继续不使用API​​(只是因为除非您登录才能继续)或致电startResolutionForResult(Activity, int)提示用户登录,但最简单的方法是add the account to your device

答案 2 :(得分:1)

你可以在https://stackoverflow.com/a/41658044/4448757引用我的答案来连接谷歌帐户,这只适用于签名的apk

然后你可以去谷歌驱动器

   IntentSender intentSender = Drive.DriveApi
            .newOpenFileActivityBuilder()
            .build(mGoogleApiClient);
    try {
        startIntentSenderForResult(intentSender, DRIVE_CHOOSER_REQUEST, null, 0, 0, 0);
    } catch (IntentSender.SendIntentException e) {
        e.printStackTrace();
    }

答案 3 :(得分:0)

不确定是否有人仍然需要这个,但在我的情况下,我有两个谷歌帐户,所以我首先需要选择我想要使用的帐户。我在onConnectionFailed

中使用了此代码
    if (!connectionResult.hasResolution())
    {
        // show the localized error dialog.
        GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), mContext, 0).show();
        return;
    }

    try
    {
        connectionResult.startResolutionForResult(mContext, 1);
    } catch (IntentSender.SendIntentException e)
    {
        e.printStackTrace();
    }