错误:ConnectionResult {statusCode = INTERNAL_ERROR,resolution = null}

N S*_*rma 17 android google-drive-api

我正在开发我在我的应用程序中集成G​​oogle云端硬盘的应用程序.下面是我的代码,我只是从示例代码中复制,但在与Google云端硬盘连接时出现异常.

例外:ConnectionResult{statusCode=INTERNAL_ERROR, resolution=null}在onConnectionFailed()方法中.

请各位分享您的观点.

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.");
    }

}
Run Code Online (Sandbox Code Playgroud)

Dan*_*l F 25

当心!开发人员控制台中存在问题.

如果你在1)确保你已经用相应的证书指纹注册了包名,并且2)(重新)使用已经存在的项目之后你就会收到这个bug ,那么你应该检查这个项目是否有产品名称和一个与之关联的电子邮件地址(仔细检查一个),两者都可以在" 同意屏幕 "部分找到.

非常旧的项目可能没有填充这两个字段.较新的项目将这些字段填入一些默认值.

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

在此输入图像描述

  • GDC的UI发生了变化.它现在位于*凭据* - >*OAuth许可屏幕* (7认同)
  • @qkx是的,它非常令人沮丧,因为你当然在寻找超级混乱的android代码中的编程错误(我需要使用哪种身份验证方法?我使用的是正确的吗?我正确使用它吗?)这是一个如果事实证明原始问题纯粹是行政问题,真正的痛苦.至少你已经清理了你的代码,对吧?;-) (3认同)

小智 10

我按照以下步骤在API控制台上签署Google云端硬盘应用程序来解决此问题

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

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

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


N S*_*rma 9

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

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

我按照上面的链接,它解决了我的问题.