【问题标题】:Can't update Google Drive file无法更新 Google 云端硬盘文件
【发布时间】:2016-03-03 12:58:01
【问题描述】:

我正在尝试使用 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())
        ]
    );

我收到以下异常:

调用 PATCH https://www.googleapis.com/upload/drive/v3/files/ABCDe4FGHvXZTKVOSkVETjktNE0?uploadType=multipart 时出错:(403) 资源主体包含不可直接写入的字段。

【问题讨论】:

  • 如果你只是删除data这个额外的参数呢?
  • 同样的问题,所以我猜这不是 data 参数造成的。将编辑问题。
  • 另外,update 方法的签名是什么?
  • public function update($fileId, Google_Service_Drive_DriveFile $postBody, $optParams = array())

标签: php google-drive-api


【解决方案1】:

显然问题是由$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'
    ]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多