简体   繁体   中英

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

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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