繁体   English   中英

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

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

我正在尝试使用 Drive Python API 公开一些幻灯片和电子表格。 首先我上传文件,然后我获取 id 以应用修订更新并且没有发生错误,但未应用更改。 我也使用了API 资源管理器,但发生了同样的情况。 我还使用了指定的修订 ID 而不是“头”,没有任何区别。 关于可能发生什么的任何想法?

我的代码:

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)

注意事项

让我们看一下您用于更新head修订的参数的文档。

已发布:此修订版是否已发布。 这仅被填充并且只能针对 Google Docs 进行修改。


publishAuto :是否会自动>重新发布后续修订版。 这仅被填充并且只能针对 Google Docs 进行修改。

如您所见,只有 Google Docs 可以设置此参数。 由于您使用您的代码上传了一个非 Google-Docs mime 类型的文件,因此您将无法发布它。

解决方案

在上传过程中将文件转换为 Google Doc 怎么样?

这是建议的修改:

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

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

参考

使用 Google Drive API v3 导入到 Google Docs 类型

修订更新

暂无
暂无

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

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