无法在服务器端将谷歌幻灯片导出为 pdf 格式



这是我在服务器端测试从谷歌云端硬盘导出文件的代码。

import logging
from flask import Flask, render_template, request
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from oauth2client.client import AccessTokenCredentials
import httplib2
import io

app = Flask(__name__)

@app.route('/gdrive/selectcallback')
def userselectioncallback():
print "In user selection callback... "
code = request.args.get('user_token')
fileId = request.args.get('fileId')
credentials = AccessTokenCredentials(code,
'my-user-agent/1.0')

http = httplib2.Http()
http_auth = credentials.authorize(http)
drive_service = build('drive', 'v3', http=http_auth)
drive_request = drive_service.files().export(
fileId=fileId,
mimeType='application/pdf')
fh = io.FileIO('test.pdf', 'wb')
downloader = MediaIoBaseDownload(fh, drive_request)
done = False
while done is False:
status, done = downloader.next_chunk()
print "Download %d%%." % int(status.progress() * 100)
return code

if __name__ == '__main__':
# This is used when running locally. Gunicorn is used to run the
# application on Google App Engine. See entrypoint in app.yaml.
app.run(host='127.0.0.1', port=8090, debug=True)

在 Web 客户端,一旦用户从文件选取器中选择一个文件,javascript 前端将使用令牌和文件 ID 调用上述 python 代码中的/gdrive/selectcallback

例如,令牌如下所示:ya29.Glu5BG-LQJFqZ-e4uImMSxz-14iS41jVLfXk6rVKvAPjylCwhUh98ZJk1iIC5Eb49pTfflGnU6qE7uzK44AYr0Wn79QMUkF368WFaYrhidrvpVjcsJSZ9P1M8VU6和文件 ID 如下所示1ON9kGyb02TFCygy8jeIYyo2BKj5SzKgAP0xi5Rm08D4

以下是相关的前端代码(在咖啡脚本中(:

pickerCallback = () ->
view   = new google.picker.View(google.picker.ViewId.PRESENTATIONS)
picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.setAppId('zeetings')
.setOAuthToken(oauthToken)
.addView(view)
.setDeveloperKey(env['googleapi-client'].apiKey)
.setCallback(selectCallback)  # The callback calls the python backend
.build()
picker.setVisible true
selectCallback = (data) ->
if data.action is google.picker.Action.PICKED
fileId = data.docs[0].id
fileSelectedCallback(fileId, oauthToken) if fileSelectedCallback

根据调试信息,我的 python 代码发出这两个 https 调用:

2017-09-01 11:32:38,810 PID 260 TID 140546358265600 正在
请求的信息发现 URL:GET https://www.googleapis.com/discovery/v1/apis/drive/v3/rest

2017年09月01日 11:32:39,009 pid 260 tid 140546358265600 信息发现网址 被请求:获取 https://www.googleapis.com/drive/v3/files/1ON9kGyb02TFCygy8jeIYyo2BKj5SzKgAP0xi4Rm08D4/export?mimeType=application%2Fpdf

如果我直接在浏览器中使用第二个 url,则会出现以下错误:

{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}

(我认为上述错误消息实际上并不反映根本原因。这更有可能是因为呼叫未在我的浏览器中进行身份验证。

我怀疑我必须使用google-auth库(https://google-auth.readthedocs.io/en/latest/user-guide.html#making-authenticated-requests(,但我不确定如何将google-auth与上面的python代码相结合。我想我可以通过以下方式获得证书

from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file(
'/path/to/key.json')

但是在此之后我应该如何处理credentials? 我用它来完全替换credentials = AccessTokenCredentials(code,'my-user-agent/1.0')吗?

附言

根据@Tanaike的建议,我尝试直接使用 API URL。这是我得到的结果:

{
"error": {
"errors": [
{
"domain": "global",
"reason": "fileNotDownloadable",
"message": "Only files with binary content can be downloaded. Use Export with Google Docs files.",
"locationType": "parameter",
"location": "alt"
}
],
"code": 403,
"message": "Only files with binary content can be downloaded. Use Export with Google Docs files."
}
}

这似乎是一个 v3 API 问题。如果我切换到 v2 并使用 downloadUrl 链接,我可以下载 pdf 格式的文件。

用户@Tanaike给了我很多很好的建议来调试这个问题。我能够直接测试 REST API 以验证 1( 我有正确的访问代码,2( 驱动器 v3 文件导出 API 按预期工作

事实证明,问题出在MediaIoBaseDownload类上。如果我将其从代码中删除并直接接收数据:

data = drive_service.files().export(
fileId=fileId,
mimeType='application/pdf').execute()
f = open('test.pdf)
f.write(data)
f.close()

然后它按预期工作

最新更新