Search code examples
androidgoogle-drive-apigoogle-signingoogle-drive-android-api

Unable to overwrite files in Google Drive AppData folder INTERNAL_ERROR


I am using GDAA in my application for managing my application files in google drive. All the below listed operations work fine like

  • google sign-in (scope is added for AppData Folder)
  • download file from AppData Folder
  • upload file to AppData Folder
  • delete file from AppData Folder

but when I try to overwrite a file in AppData Folder i am getting the following error in the onResult() callback.

Status Message : Failed to commit changes. 
Status Code : INTERNAL_ERROR (8)

I am unable to understand why this is happening. Please find below my code for reference

public void overwrite(String strLocalFilePath, String strDriveId, String strGoogleDriveFileMimeType, String strGoogleDriveFileTitle){
    final DriveId driveId = DriveId.decodeFromString(strDriveId);
    DriveFile file = driveId.asDriveFile();

    file.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
        @Override
        public void onResult(DriveApi.DriveContentsResult result) {
            if (!result.getStatus().isSuccess()) {
                Log.e(TAG,"Error");
                return;
            }

            DriveContents driveContents = result.getDriveContents();
            OutputStream outputStream = driveContents.getOutputStream();
            boolean isSuccess = writeFileToStream(outputStream, strLocalFilePath);

            if (isSuccess) {
                MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                        .setTitle(strGoogleDriveFileTitle)
                        .setMimeType(strGoogleDriveFileMimeType)
                        .build();

                ExecutionOptions executionOptions = new ExecutionOptions.Builder()
                        .setNotifyOnCompletion(true)
                        .setTrackingTag("SAMPLE_TRACKING_TAG")
                        .build();

                driveContents.commit(mGoogleApiClient, changeSet, executionOptions).setResultCallback(new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        // Handle the response status
                        if (!status.getStatus().isSuccess()) {
                            Log.e(TAG, "Error while trying to overwrite file. Message : "+status.getStatus().getStatusMessage() + " Status code : "+status.getStatus().getStatusCode());
                            return;
                        }else{
                            Log.d(TAG,"File overwritten successfully!!");
                        }

                    }
                });
            } else {
                Log.e(TAG, "File I/O Error occurred : "+ strGoogleDriveFileTitle);
            }
        }
    });
}

private boolean writeFileToStream (OutputStream oos, String filePath){
    if (oos != null) {
        InputStream is = null;
        try {
            Log.d(TAG, "Started writing file : "+filePath);
            is = new FileInputStream(filePath);
            byte[] buf = new byte[4096];
            int c;
            while ((c = is.read(buf, 0, buf.length)) > 0) {
                oos.write(buf, 0, c);
                oos.flush();
            }
            Log.d(TAG, "Finished writing file : "+filePath);
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if(oos != null) {
                    oos.close();
                }
                if(is != null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
    }
    return false;
}

Solution

  • According to this thread, if you get error code 8 (INTERNAL_ERROR), please double check your app registration in dev console. Note that every registered Android client is uniquely identified by the (package name, Android Signing Certificate SHA-1) pair. If you have multiple package names / signing certificate for your debug and production environments, make sure to register every pair of them.

    To verify:

    1. Open the Credentials page and select your project
    2. Make sure every pair has an Android typed OAuth 2.0 client IDs. To create a new OAuth 2.0 client ID for your Android client, select New Credentials->OAuth2 Client ID from the dropdown, select Android and input your Package name / Signing-certificate fingerprint there.

    To get your signing key certificate SHA-1:

    Standard Debug Key

    keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
    

    Other (Custom) Key

    keytool -list -v -keystore $YOUR_KEYSTORE_LOCATION
    

    Here's a reference which might also help:

    If you are getting this bug even after 1) making sure that you have registered the package name with its corresponding certificate fingerprint, and 2) are (re)using an already existing project, then you should check that this project has an product name and an email address (double check that one specially) associated with it, both to be found in the "consent screen" section.