Google Drive API v3 更改文件的元数据

发布于 2025-01-13 23:20:56 字数 343 浏览 4 评论 0原文

我在更改谷歌驱动器上文件的元数据时遇到问题。我得到了从驱动器下载文件的脚本,下载后我想用星号标记文件(星号= True)。

我找到了 v2 API 的解决方案,但它不适用于 v3,我收到错误“资源主体包含不可直接写入的字段”。

我阅读了 v3 的文档,但没有找到如何更改元数据的解决方案。

file = service.files().get(fileId=file_id).execute()
file['starred'] = True
service.files().update(fileId=file_id, body=file).execute()

感谢您的任何帮助。

i got a problem with changing metada of file on google drive. I got script for downloading files from drive and after download i want to mark file with star (starred = True).

I found solution for v2 API, but its not working for v3, where i reciving error "The resource body includes fields which are not directly writable".

I read a documentation for v3 but didnt find solution how to change metadata.

file = service.files().get(fileId=file_id).execute()
file['starred'] = True
service.files().update(fileId=file_id, body=file).execute()

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2025-01-20 23:20:56

您遇到的问题是您首先执行 file.get 。

file = service.files().get(fileId=file_id).execute()

这将填充完整的文件资源对象。由于更新方法使用补丁方法,它会尝试更新您发送的每个属性,其中一些属性不可写。您应该只发送您想要更新的字段。

试试这个。

file_metadata = {'starred': true}

updated_file = service.files().update(
            fileId='id_file_in_google_drive',
            body=file_metadata ).execute()

The issue you are having is you are doing a file.get first.

file = service.files().get(fileId=file_id).execute()

This populates the full file resource object. And since the update method uses patch methodology its trying to update every property that you sent and some of them are not writeable. you should only send the fields you want to update.

Try this.

file_metadata = {'starred': true}

updated_file = service.files().update(
            fileId='id_file_in_google_drive',
            body=file_metadata ).execute()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文