简体   繁体   English

Google Drive Python API - 修订版更新不会对文件应用更改

[英]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.我正在尝试使用 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.首先我上传文件,然后我获取 id 以应用修订更新并且没有发生错误,但未应用更改。 I also used the API explorer but the same happens.我也使用了API 资源管理器,但发生了同样的情况。 I have also used the assigned revisionId instead of 'head' with no difference.我还使用了指定的修订 ID 而不是“头”,没有任何区别。 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.让我们看一下您用于更新head修订的参数的文档。

published : Whether this revision is published.已发布:此修订版是否已发布。 This is only populated and can only be modified for Google Docs.这仅被填充并且只能针对 Google Docs 进行修改。


publishAuto : Whether subsequent revisions will be automatically >republished. publishAuto :是否会自动>重新发布后续修订版。 This is only populated and can only be modified for Google Docs.这仅被填充并且只能针对 Google Docs 进行修改。

As you can see only Google Docs can have this parameters set.如您所见,只有 Google Docs 可以设置此参数。 Since with your code you are uploading a file with a non-Google-Docs mime Type you won't be able to publish it.由于您使用您的代码上传了一个非 Google-Docs mime 类型的文件,因此您将无法发布它。

Solution解决方案

What about converting the file to a Google Doc during the upload?在上传过程中将文件转换为 Google Doc 怎么样?

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.file_metadata指定mimeType将启用从.xlsx到 Google 电子表格文件的转换。 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.现在使用修订更新端点将导致填充已publishedpubilshAuto字段,您将能够看到已发布的文件。

References参考

Import to Google Docs types with Google Drive API v3 使用 Google Drive API v3 导入到 Google Docs 类型

Revisions update 修订更新

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

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