【问题标题】:Error : ConnectionResult{statusCode=INTERNAL_ERROR, resolution=null}错误:ConnectionResult{statusCode=INTERNAL_ERROR, resolution=null}
【发布时间】:2014-02-12 09:14:17
【问题描述】:

我正在开发将 Google Drive 集成到我的应用程序中的应用程序。下面是我的代码,我只是从示例代码中复制而来,但在连接 Google Drive 时出现异常。

异常:onConnectionFailed() 方法中的ConnectionResult{statusCode=INTERNAL_ERROR, resolution=null}

请大家分享你的观点。

public class MainActivity extends Activity implements ConnectionCallbacks,
        OnConnectionFailedListener {

    private static final String TAG = "android-drive-quickstart";
    private static final int REQUEST_CODE_CAPTURE_IMAGE = 1;
    private static final int REQUEST_CODE_CREATOR = 2;
    private static final int REQUEST_CODE_RESOLUTION = 3;

    private GoogleApiClient mGoogleApiClient;
    private Bitmap mBitmapToSave;

    /**
     * Create a new file and save it to Drive.
     */
    private void saveFileToDrive() {
        // Start by creating a new contents, and setting a callback.
        Log.i(TAG, "Creating new contents.");
        final Bitmap image = mBitmapToSave;
        Drive.DriveApi.newContents(mGoogleApiClient).addResultCallback(new OnNewContentsCallback() {

            @Override
            public void onNewContents(ContentsResult result) {
                // If the operation was not successful, we cannot do anything
                // and must
                // fail.
                if (!result.getStatus().isSuccess()) {
                    Log.i(TAG, "Failed to create new contents.");
                    return;
                }
                // Otherwise, we can write our data to the new contents.
                Log.i(TAG, "New contents created.");
                // Get an output stream for the contents.
                OutputStream outputStream = result.getContents().getOutputStream();
                // Write the bitmap data from it.
                ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
                try {
                    outputStream.write(bitmapStream.toByteArray());
                } catch (IOException e1) {
                    Log.i(TAG, "Unable to write file contents.");
                }
                // Create the initial metadata - MIME type and title.
                // Note that the user will be able to change the title later.
                MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                        .setMimeType("image/jpeg").setTitle("Android Photo.png").build();
                // Create an intent for the file chooser, and start it.
                IntentSender intentSender = Drive.DriveApi
                        .newCreateFileActivityBuilder()
                        .setInitialMetadata(metadataChangeSet)
                        .setInitialContents(result.getContents())
                        .build(mGoogleApiClient);
                try {
                    startIntentSenderForResult(
                            intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
                } catch (SendIntentException e) {
                    Log.i(TAG, "Failed to launch file chooser.");
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        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
    protected void onPause() {
        if (mGoogleApiClient != null) {
            mGoogleApiClient.disconnect();
        }
        super.onPause();
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_CAPTURE_IMAGE:
                // Called after a photo has been taken.
                if (resultCode == Activity.RESULT_OK) {
                    // Store the image data as a bitmap for writing later.
                    mBitmapToSave = (Bitmap) data.getExtras().get("data");
                }
                break;
            case REQUEST_CODE_CREATOR:
                // Called after a file is saved to Drive.
                if (resultCode == RESULT_OK) {
                    Log.i(TAG, "Image successfully saved.");
                    mBitmapToSave = null;
                    // Just start the camera again for another photo.
                    startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                            REQUEST_CODE_CAPTURE_IMAGE);
                }
                break;
        }
    }

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

    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "API client connected.");
        if (mBitmapToSave == null) {
            // This activity has no UI of its own. Just start the camera.
            startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                    REQUEST_CODE_CAPTURE_IMAGE);
            return;
        }
        saveFileToDrive();
    }

    @Override
    public void onDisconnected() {
        Log.i(TAG, "API client disconnected.");
    }

}

【问题讨论】:

标签: android google-drive-api


【解决方案1】:

请注意!开发者控制台出现问题。

如果您在 1) 确保您已使用其对应的 证书指纹 注册 包名称 并且 2) 正在(重新)使用之后仍遇到此错误一个已经存在的项目,那么你应该检查这个项目有一个产品名称和一个电子邮件地址(双检查与它相关的一个特别),两者都可以在“同意屏幕”部分中找到。

非常古老的项目可能没有填充这两个字段。较新的项目在这些字段中填写了一些默认值。

我花了一天时间才找到这个......

【讨论】:

  • 顺便说一句,如果您选择了稍后从访问权限中删除的电子邮件地址,这可能也会发生。不过,尚未对此进行验证。
  • 只需添加电子邮件和产品名称即可解决我的 GooglePlayServicesUtil 内部错误问题。谢谢!
  • 丹尼尔的回答太棒了!其实你的答案是正确的。当然,谷歌不知何故忘记在文档中提到你还需要填写同意屏幕,我花了 2 天的时间才发现为什么即使我完全按照所有步骤复制粘贴官方谷歌演示也不起作用......谢谢!
  • @qkx 是的,这非常令人沮丧,因为您当然是在寻找超级混乱的 android 代码中的编程错误(我需要使用哪种身份验证方法?我使用的是正确的吗?我我正确地使用它吗?)如果事实证明最初的问题是纯粹的管理问题,那将是一个真正的痛苦。至少你已经清理了你的代码,对吧? ;-)
  • GDC 的 UI 发生了变化。它现在位于 Credentials -> OAuth 同意屏幕
【解决方案2】:

我通过按照以下步骤在 API 控制台上签署我的 Google Drive 应用程序来解决此问题

  1. 转到 Google Developers Console。
  2. 选择一个项目,或创建一个新项目。
  3. 在左侧边栏中,展开 APIs & auth。接下来,点击 API。
  4. 在 API 列表中,确保 Drive API 的状态为开启。
  5. 在左侧边栏中,选择凭据。

如果您的应用需要提交授权请求:

  1. 在 OAuth 下,点击创建新客户端 ID。
  2. 选择已安装的应用程序和 Android。
  3. 在包名称字段中,输入您的 Android 应用的包名称。
  4. 将 SHA1 指纹粘贴到请求的表单中。
  5. 点击创建客户端 ID。

【讨论】:

  • "4. 在 API 列表中,确保 Drive API 的状态为 ON。"这解决了我的问题。谢谢。
【解决方案3】:

我通过注册应用程序和生成签名证书指纹解决了这个问题。

https://developers.google.com/drive/android/auth#generate_the_signing_certificate_fingerprint_and_register_your_application

我按照上面的链接解决了我的问题。

【讨论】:

    【解决方案4】:

    对我来说,问题是在示例中有:

    .addApi(Drive.API)
    

    而且我没有在控制台中添加驱动api

    此错误消息帮助我找出问题

    com.google.android.gms.drive.auth.c:授权失败:服务器 返回错误:未配置访问。您的 API 未启用 项目,或者配置了 per-IP 或 per-Referer 限制 您的 API 密钥和请求与这些限制不匹配。请 使用 Google Developers Console 更新您的配置。请参阅 https://developers.google.com/drive/handle-errors了解详情。

    【讨论】:

    • 对我来说这也是一个问题。另一个问题是我在 Manifest.xml 中的数据包名称与开发人员控制台中引入的不同。
    【解决方案5】:

    不要忘记权限:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    

    【讨论】:

      【解决方案6】:

      您在 logcat 中有任何错误消息吗?连接失败的最可能原因是您没有在云控制台中正确设置应用程序。

      在此处查看说明:https://developers.google.com/drive/android/auth

      【讨论】:

      • 该错误消息是 logcat 条目...如果它更详细一点就好了。
      • 这当然是文档中写的“必须具备”条件。但是,除非您遵循 Daniel F 的建议并在同意屏幕中填写电子邮件,否则它仍然无法工作 :) 当然,Google 不知何故忘记提及它,我杀死了 2 天,发现为什么即使我遵循所有复制粘贴官方 google 演示也无法正常工作精确的步骤...
      【解决方案7】:

      就我而言,我必须更改客户端 ID 以使用构建 GoogleApiClient 对象的活动类的确切包名称,而不是更高级别的包。

      【讨论】:

        【解决方案8】:

        我也遇到了同样的错误。 对于我的项目,在 API & auth -> APIs 下未启用 Drive API。 启用此 Drive API 后,此问题已解决。

        【讨论】:

          【解决方案9】:

          我解决了将这一行添加到 gradle:

          compile 'com.google.android.gms:play-services-identity:8.1.0'
          

          【讨论】:

            【解决方案10】:

            您需要有两个单独的客户端 ID,一个用于调试,另一个用于发布。 有时我们会错过显而易见的事情。

            【讨论】:

              【解决方案11】:

              在 Android Studio 中转到

              工具-> Android-SDK 管理器 -> Google Play 服务

              更新 Google Play 服务..我相信它会起作用

              【讨论】:

                【解决方案12】:

                当我将现有应用程序从 Eclipse 移动到 Android Studio 时,我遇到了与上述相同的问题。我的问题是我命名的 applicationId 与包 ID 不同。将 applicationId 更改为与包名相同即可解决问题。

                【讨论】:

                  【解决方案13】:

                  我遇到了同样的问题。我的登录工作正常,但突然停止工作。我尝试了这里提到的所有解决方案。但没有解决我的问题。但是,禁用谷歌登录并启用它解决了我的问题。所以只要去firebase控制台,选择Authentication > Sign-in Methods > google > 点击Edit > 禁用和启用

                  【讨论】:

                    猜你喜欢
                    • 2016-04-25
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-03-31
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-01-03
                    • 2017-03-19
                    • 1970-01-01
                    相关资源
                    最近更新 更多