简体   繁体   English

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

[英]Google Drive API v3 changing metadata of file

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".我找到了 v2 API 的解决方案,但它不适用于 v3,我收到错误“资源主体包含不可直接写入的字段”。

I read a documentation for v3 but didnt find solution how to change metadata.我阅读了 v3 的文档,但没有找到如何更改元数据的解决方案。

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

Thanks for any help.谢谢你的帮助。

The issue you are having is you are doing a file.get first.您遇到的问题是您正在执行 file.get 首先。

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.这将填充完整的文件资源 object。并且由于更新方法使用补丁方法,它会尝试更新您发送的每个属性,其中一些是不可写的。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM