Google 驱动器 API PHP - 更新 txt 文件的内容
Google Drive API PHP - update content of txt file
我创建了一个文件夹,并在该文件夹中创建了 2 个文本文件,其中包含一些文本。我还可以毫无问题地列出文件并获取其中的文本。现在我正在尝试更新该文件中的文本,但我总是收到此错误:
"domain": "global",
"reason": "fieldNotWritable",
"message": "The resource body includes fields which are not directly writable."
我创建了一个类似于 https://developers.google.com/drive/v2/reference/files/update 中介绍的函数。
我使用这个例子是因为在 v3 版本中,Google 没有提供任何例子,而且我找不到任何可以帮助我的东西。我的功能如下。
function updateFile ($service, $fileId, $newTitle, $newDescription, $newMimeType, $text) {
try {
// First retrieve the file from the API.
$file = $service->files->get($fileId);
// File's new metadata.
$file->setName($newTitle);
$file->setDescription($newDescription);
$file->setMimeType($newMimeType);
// File's new content.
$additionalParams = array(
'data' => $text,
'uploadType' => 'media'
);
// Send the request to the API.
$updatedFile = $service->files->update($fileId, $file, $additionalParams);
return $updatedFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
提前致谢。
基于此 ,在 v3 上,像这样使用 update
会引发错误 fieldNotWritable
。
The solution is to create an empty File
setting only the new values:
File newContent = new File();
newContent.setTrashed(true);
service.files().update(fileId, newContent).execute();
Note: File
refers to com.google.api.services.drive.model.File
(it is not java.io.File
).
另外,基于此documentation, you can see that shared isn't a writable field. You can share a file by adding a new permission, and you can check if a file has been shared by reading the shared property. But saying a file is shared, other than by actually sharing it, makes no sense.
我创建了一个文件夹,并在该文件夹中创建了 2 个文本文件,其中包含一些文本。我还可以毫无问题地列出文件并获取其中的文本。现在我正在尝试更新该文件中的文本,但我总是收到此错误:
"domain": "global",
"reason": "fieldNotWritable",
"message": "The resource body includes fields which are not directly writable."
我创建了一个类似于 https://developers.google.com/drive/v2/reference/files/update 中介绍的函数。
我使用这个例子是因为在 v3 版本中,Google 没有提供任何例子,而且我找不到任何可以帮助我的东西。我的功能如下。
function updateFile ($service, $fileId, $newTitle, $newDescription, $newMimeType, $text) {
try {
// First retrieve the file from the API.
$file = $service->files->get($fileId);
// File's new metadata.
$file->setName($newTitle);
$file->setDescription($newDescription);
$file->setMimeType($newMimeType);
// File's new content.
$additionalParams = array(
'data' => $text,
'uploadType' => 'media'
);
// Send the request to the API.
$updatedFile = $service->files->update($fileId, $file, $additionalParams);
return $updatedFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
提前致谢。
基于此 update
会引发错误 fieldNotWritable
。
The solution is to create an empty
File
setting only the new values:File newContent = new File(); newContent.setTrashed(true); service.files().update(fileId, newContent).execute();
Note:
File
refers tocom.google.api.services.drive.model.File
(it is notjava.io.File
).
另外,基于此documentation, you can see that shared isn't a writable field. You can share a file by adding a new permission, and you can check if a file has been shared by reading the shared property. But saying a file is shared, other than by actually sharing it, makes no sense.