无法导出为服务器端的PDF格式的谷歌幻灯片
这里是我的代码来测试从谷歌驱动器在服务器端导出文件。无法导出为服务器端的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前端将调用/gdrive/selectcallback
与令牌和文件ID以上Python代码。
举个例子,令牌看起来是这样的:ya29.Glu5BG-LQJFqZ-e4uImMSxz-14iS41jVLfXk6rVKvAPjylCwhUh98ZJk1iIC5Eb49pTfflGnU6qE7uzK44AYr0Wn79QMUkF368WFaYrhidrvpVjcsJSZ9P1M8VU6
和文件ID看起来像这样1ON9kGyb02TFCygy8jeIYyo2BKj5SzKgAP0xi5Rm08D4
下面是相关前端代码(在CoffeeScript中):基于
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 INFO
discovery URL being requested: GET https://www.googleapis.com/discovery/v1/apis/drive/v3/rest2017-09-01 11:32:39,009 pid 260 tid 140546358265600 INFO discovery URL being requested: GET https://www.googleapis.com/drive/v3/files/1ON9kGyb02TFCygy8jeIYyo2BKj5SzKgAP0xi4Rm08D4/export?mimeType=application%2Fpdf
如果我使用第二个url直接在一个br owser,我得到以下错误:
{
"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')
?
P.S.
根据@ 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格式的文件。
User @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()
那么它将按预期工作
你从导出API得到了什么错误消息?通过API浏览器导出似乎工作正常 –
它发生在问题中提到的特定幻灯片(也提供了链接)。 “下载0%”被打印出来很多时间,直到最后抓到一个500. –
您可能想要检查这个报告为[issue](https://issuetracker.google.com/issues/64294118)哪个Drive API返回错误500 。目前尚不清楚它是否正在为v3工作,因为报告只证实它在v2中工作。您可能想要关注该问题,然后发表评论/创建一个新问题。希望得到这个帮助 –