Google_Service_Exception:在Google云端硬盘上上传文件新版本时的fieldNotWritable

Google_Service_Exception:在Google云端硬盘上上传文件新版本时的fieldNotWritable

问题描述:

我的意图是能够上载Google云端硬盘中已有的新版本文件。Google_Service_Exception:在Google云端硬盘上上传文件新版本时的fieldNotWritable

我已经从PHP示例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; 
    } 
} 

结果我得到

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." 
} 
} 

我不明白什么是有问题的不是写的领域。我正在改变的唯一事情就是文件的实际内容,而不是它的元数据。

任何想法有什么不对?

+0

可能与API的v3中的更改有任何关系? – Slawa

看看$file = $this->service->files->get($fileID)返回的$file对象。我的猜测是,它包含一堆未定义为可写入的字段https://developers.google.com/drive/v3/reference/files

因此,当您向$file = $this->service->files->update($fileID, $file,中的驱动器发送相同的$文件对象时,Drive会反对它们的存在。由于您只更新内容,因此您可以发送一个空的元数据对象来代替$ file。另外,正如Siawa指出的那样,您所追随的快速入门标签为v2,但如果您使用的是最新的PHP库,则可能已更改为v3。任何人的猜测都是否影响快速入门。