【问题标题】:Editing Google docs with drive API使用驱动 API 编辑 Google 文档
【发布时间】:2012-09-10 13:06:34
【问题描述】:

(为清楚起见,这篇文章涉及到 Google Documents List APIGoogle Drive API 与 Python 在 Google App Engine 上的区别)

使用 [现已弃用] 文档列表 API,我可以通过导出为 HTML、修改 HTML 然后重新上传来编辑 Google 文档,无论是作为新文档还是作为对原始文档的修改。这对于从模板生成 PDF 文档等事情很有用。我一直在尝试使用新的 Drive API (V2) 复制此功能,但似乎无法做到。

想出了这个...

http = # authenticated http client
drive_service = build('drive', 'v2', http=http)

# get the file ...
file = drive_service.files().get(fileId=FILE_ID).execute()

# download the html ...
url = file['exportLinks']['text/html']
response, content = http.request(url)

# edit html
html = content.replace('foo', 'bar')

# create new file with modified html ...
body = {'title':'Modified Doc', 
        'description':'Some description', 
        'mimeType':'text/html'}
media_body = MediaIoBaseUpload(StringIO.StringIO(html), 'text/html', resumable=False)
drive_service.files().insert(body=body, media_body=media_body)

上述代码将 html 文件作为文件上传到 Google Drive,而不是将 HTML 呈现为 Google Document。很公平,这是有道理的。但是,如何像使用 Documents List API 那样将其呈现为 Google Doc?

另一件事 - 如果我设置 resumable=True 它会在 App Engine 上引发以下错误 - '_StreamSlice' 没有 len()。不知道如何让 resumable=True 工作?

最后一件事 - sample code in the docs 使用 MediaInMemoryUpload 对象,但 if you look at the source 现在已弃用,取而代之的是 MediaIoBaseUpload。示例代码应该更新吗?!

【问题讨论】:

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


    【解决方案1】:

    我怀疑问题在于转换的默认值已从 true 更改为 false。您必须在上传时明确设置 convert=true。见https://developers.google.com/drive/v2/reference/files/insert

    【讨论】:

    • 但是你在哪里设置 convert=true?没有一个示例描述参数的设置位置。修补 service.files().insert().execute.uri 会引发错误,并且 files 和 insert 似乎都没有正确的参数。 ruby 版本的 apiclient 将参数参数传递给 .execute()。
    • 作为参数传递给插入方法。就像这个 drive_service.files().insert(body=body, media_body=media_body, convert=True).execute()。它记录在这里developers.google.com/drive/v2/reference/files/insert
    • 没关系,我回答了我自己的问题。你使用类似:service.files().insert(body=resource, media_body=media, convert=True).execute()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多