簡體   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