1

I created one folder and inside that folder I've created 2 text files, with some text inside. I can also list the files and get the text in them without problems. Now I am trying to update the text inside of that files, but I am always getting this error:

"domain": "global",
"reason": "fieldNotWritable",
"message": "The resource body includes fields which are not directly writable."

I created a function similar to the one presented in https://developers.google.com/drive/v2/reference/files/update.

I used this example because in version v3, Google don't present any example, and I can't find anything that can help me in this. My function is below.

function updateFile ($service, $fileId, $newTitle, $newDescription, $newMimeType, $text) {
    try {
        // First retrieve the file from the API.
        $file = $service->files->get($fileId);

        // File's new metadata.
        $file->setName($newTitle);
        $file->setDescription($newDescription);
        $file->setMimeType($newMimeType);

        // File's new content.
        $additionalParams = array(
            'data' => $text,
            'uploadType' => 'media'
        );

        // Send the request to the API.
        $updatedFile = $service->files->update($fileId, $file, $additionalParams);
        return $updatedFile;
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}

Thank you in advance.

abielita
  • 13,147
  • 2
  • 17
  • 59
Paulo Pereira
  • 211
  • 1
  • 8

1 Answers1

0

Based from this SO answer, on v3, using update like that throws the error fieldNotWritable.

The solution is to create an empty File setting only the new values:

File newContent = new File();
newContent.setTrashed(true);
service.files().update(fileId, newContent).execute();

Note: File refers to com.google.api.services.drive.model.File (it is not java.io.File).

Also, based from this documentation, you can see that shared isn't a writable field. You can share a file by adding a new permission, and you can check if a file has been shared by reading the shared property. But saying a file is shared, other than by actually sharing it, makes no sense. [Source.]

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59