【问题标题】:Google Drive Python API - Revision update does not apply changes to a fileGoogle Drive Python API - 修订更新不会将更改应用于文件
【发布时间】:2020-11-04 17:08:24
【问题描述】:

我正在尝试使用 Drive Python API 公开一些幻灯片和电子表格。首先我上传文件,然后我得到 id 来应用修订更新并且没有发生错误,但没有应用更改。我也使用了API explorer,但同样的情况发生了。我也使用了分配的 revisionId 而不是“head”,没有区别。对可能发生的事情有任何想法吗?

我的代码:

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)

【问题讨论】:

  • 你到底做了哪些改变?
  • @DaImTo 上传文件后,我更新了头部修订,将published 字段更改为True,以便公开文档,类似于“发布到网络”下的Google 表格/演示文稿的文件菜单

标签: python google-drive-api client google-api-python-client


【解决方案1】:

注意事项

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

published :此修订是否已发布。 这仅是填充的,并且只能针对 Google 文档进行修改。


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

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

解决方案

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

这是建议的修改:

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

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

参考

Import to Google Docs types with Google Drive API v3

Revisions update

【讨论】:

  • 谢谢@Alessandro,我稍后再试,我会告诉你发生了什么。
猜你喜欢
  • 1970-01-01
  • 2014-03-22
  • 2013-04-03
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
相关资源
最近更新 更多