我正在尝试使用v3 google drive sdk将文件上载到google drive:

$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())
        ]
    );

我收到以下异常:
调用修补程序时出错:(403)资源体包含不可直接写入的字段。

最佳答案

显然问题是由$this->report引起的,我通过以下方式得到:

// 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);

可能$this->report\Google_Service_Drive_DriveFile的一个实例,它包含一些在设置并传递给update()方法时会导致问题的字段。
我可以通过将一个新的\Google_Service_Drive_DriveFile实例传递给update()方法来解决这个问题,如下所示:
$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'
    ]);

07-24 09:47
查看更多