简体   繁体   中英

Google Drive Python API - Revision update does not apply changes to a file

I am trying to make public some slides and spreadsheets with the Drive Python API. In first place I upload the file, then I get the id to apply a revision update and no errors occur, but the changes are not applied. I also used the API explorer but the same happens. I have also used the assigned revisionId instead of 'head' with no difference. Any ideas of what could be happening?

My code:

from Google import Create_Service
from googleapiclient.http import MediaFileUpload

CLIENT_SECRET_FILE = 'credentials.json'
API_NAME = 'drive'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.appdata', 'https://www.googleapis.com/auth/drive.file']

service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

folder_id = 's0m3Id$tr1n9'
file_name = 'excel.xlsx'
file_mime_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
file_metadata = {
    'name': file_name,
    'parents': [folder_id]
}

# Uploaing file
media = MediaFileUpload(f'./uploads/{file_name}', mimetype=file_mime_type)
file_id = service.files().create(
    body = file_metadata,
    media_body = media,
    fields = 'id'
).execute()['id']
print(f'Assigned ID: {file_id}')

# Revision
public_slide = service.revisions().update(
    fileId = file_id,
    revisionId = 'head',
    body = {
        'published': True,
        'publishAuto': True
    }
).execute()
print(public_slide)

Considerations

Let's take a look at the documentation for the parameters you are using to update the head revision.

published : Whether this revision is published. This is only populated and can only be modified for Google Docs.


publishAuto : Whether subsequent revisions will be automatically >republished. This is only populated and can only be modified for Google Docs.

As you can see only Google Docs can have this parameters set. Since with your code you are uploading a file with a non-Google-Docs mime Type you won't be able to publish it.

Solution

What about converting the file to a Google Doc during the upload?

Here is the proposed modification:

file_metadata = {
    'name': file_name,
    'parents': [folder_id],
    'mimeType': 'application/vnd.google-apps.spreadsheet'
}

Specifying the mimeType in file_metadata will enable the conversion from your .xlsx to a Google Spreadsheet file. Now using the revision update endpoint will result in populating the published and pubilshAuto fields and you will be able to see your file published.

References

Import to Google Docs types with Google Drive API v3

Revisions update

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