0

My intention is to be able to upload a new version of the file which is already available on Google Drive.

I've started by the PHP example at https://developers.google.com/drive/v2/reference/files/update#examples .

function updateFile($fileID, $uploadFile) {
    try {
        $file = $this->service->files->get($fileID);

        $content = file_get_contents($uploadFile);
        $file = $this->service->files->update($fileID, $file, array(
            'data' => $content,
        ));
        printf("Updated File ID: %s\n", $file->getId());
    } catch (Exception $e) {
        echo get_class($e), ': ', $e->getMessage(), PHP_EOL;
    }
}

As a result I get

Google_Service_Exception: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "fieldNotWritable",
    "message": "The resource body includes fields which are not directly writable."
   }
  ],
  "code": 403,
  "message": "The resource body includes fields which are not directly writable."
 }
}

I don't get what are the not-writable fields in question. The only thing I'm changing is the actual content of the file, none of it's metadata.

Any ideas what's wrong?

Slawa
  • 1,141
  • 15
  • 21

1 Answers1

2

Have a look at the $file object returned by $file = $this->service->files->get($fileID). My guess is that it it contains a bunch of fields that are not defined as writeable at https://developers.google.com/drive/v3/reference/files

So when you send the same $file object to Drive in $file = $this->service->files->update($fileID, $file,, Drive is objecting to their presence. Since you are only updating the content, you can send an empty meta data object in place of $file.

Also, as Siawa pointed out, the quickstart you are following is labelled as v2, but if you are using the latest PHP library, that has probably changed to v3. Whether or not that affects the quickstart is anybody's guess.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115