【问题标题】:Android API Google Drive "connection failed"Android API Google Drive“连接失败”
【发布时间】:2015-01-18 11:51:58
【问题描述】:

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

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

但是,我遇到了无法解决的错误。

GoogleApiClient 连接失败: ConnectionResult{statusCode=SIGN_IN_REQUIRED, 分辨率=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;
    }
}

【问题讨论】:

  • 在我的情况下,这个错误是由于应用程序包的某些内容与凭据中的内容不匹配造成的。需要确保包名称和 SHA1 必须匹配。在开发机器上运行的调试版本可能与发布版本具有不同的包名称和 SHA1。

标签: android google-drive-api


【解决方案1】:

这可能是由于开发人员的疏忽(就像我发生的那样) - 您在控制台上提供的 SHA1 详细信息是生产密钥的详细信息,并且您正在调试模式下进行测试(SHA1 详细信息会有所不同)。不过,google drive API 给出的错误消息可能会更好!

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 奇怪,在我的代码中没有改变任何东西,它可以在我的索尼平板电脑上运行,但不能在三星 O.o 上运行
    • 您是否使用同一个帐户登录了两台设备?
    • 是的,同一个账号
    • 格式化后可以在我的三星平板电脑上使用。
    【解决方案3】:

    您可以在这里参考我的回答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();
        }
    

    【讨论】:

      【解决方案4】:

      不确定是否有人仍然需要这个,但就我而言,我有两个谷歌帐户,所以我首先需要选择我想使用的帐户。我在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();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-27
        • 1970-01-01
        相关资源
        最近更新 更多