谷歌驱动器sdk导出每日限制未经验证的使用



我正试图根据谷歌发布的v3示例下载/导出一个文件。我收到"超过未经验证使用的每日限额。继续使用需要注册。"错误。

我在这里和其他地方搜索过,所有的链接都表明我错过了设置凭据的机会。然而,我是在基本的快速启动示例的基础上构建的,并且能够在同一应用程序中列出我的驱动器文件夹的内容。是的,我已经将请求的范围从drive.metadata.readoly更改为drive.readoly以支持下载。我错过了什么?

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import io
from googleapiclient.http import MediaIoBaseDownload
# Setup the Drive v3 API
SCOPES = 'https://www.googleapis.com/auth/drive.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
drive_service = build('drive', 'v3', http=creds.authorize(Http()))
# Call the Drive v3 API to list first 10 items (this works)
# example from google.
results = drive_service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print('{0} ({1})'.format(item['name'], item['id']))
# Try to download the first item (it's a google doc I can edit, this FAILS)
# code pretty much lifted from google
file_id = items[0]['id']
print (file_id)
request = drive_service.files().export_media(fileId=file_id,
mimeType='application/pdf')
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print ( "Download %d%%." % int(status.progress() * 100) )

找到了。谷歌的例子是缓存凭据文件(credentials.json(。当我最初运行这个例子时,作用域权限不是drive.readonly,而是drive.metadata.readonly.我想当我更改它们时,请求不再有效。

我删除了和credentials.json并重新运行了脚本(并在浏览器上重新批准了凭据请求(,它成功了。由于BytesIO实际上并没有写入磁盘,我最终也使用了以下方法来存储数据。

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

相关内容

最新更新