3
votes

I'm trying to upload a file to Google Drive using the the v3 Google Drive SDK:

$this->drive()
    ->files
    ->update(
        $this->report->getId(),
        $this->report,  // This is a Google_Service_Drive_DriveFile instance
        [
            'uploadType' => 'multipart',
            'data' => file_get_contents($this->getLocalReportLocation())
        ]
    );

And I receive the following exception:

Error calling PATCH https://www.googleapis.com/upload/drive/v3/files/ABCDe4FGHvXZTKVOSkVETjktNE0?uploadType=multipart: (403) The resource body includes fields which are not directly writable.

1
What if you just remove the data extra parameter?Matteo Tassinari
Same issue, so I guess it's not the data parameter causing it. Will edit the question.siannone
also, what is the signature of the update method?Matteo Tassinari
public function update($fileId, Google_Service_Drive_DriveFile $postBody, $optParams = array())siannone

1 Answers

2
votes

Apparently the issue is caused by $this->report which I obtain in the following way:

// Fetch the latest synchronized report
$latestReport = $this->drive()
    ->files
    ->listFiles([
        'orderBy' => 'modifiedTime',
        'q'       => '\''.$this->userEmail.'\' in owners AND name contains \'AppName\'',
    ])
    ->getFiles()[0];

$this->report = $this->drive()
    ->files
    ->get($latestReport->id);

Probably $this->report, which is an instance of \Google_Service_Drive_DriveFile, contains some fields that are causing issues when set and passed to the update() method.

I was able to solve the problem by passing a new instance of \Google_Service_Drive_DriveFile to the update() method like this:

$this->drive()
    ->files
    ->update($this->report->getId(), (new \Google_Service_Drive_DriveFile()), [
        'uploadType' => 'multipart',
        'data' => file_get_contents($this->getLocalReportLocation()),
        'mimeType' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    ]);