google-drive-sdk导出未经授权的每日限制

时间:2018-07-14 11:43:14

标签: python google-drive-api

我正在尝试根据v3 example published by google下载/导出文件。我收到“超出未经身份验证的使用的每日限制。继续使用需要注册。”错误。

我已经在这里和其他地方搜索过,所有链接都提示我缺少设置凭据。但是,我是在基本quickstart example的基础上构建的,并且能够在同一应用程序中列出驱动器文件夹的内容。是的,我已将请求的范围从drive.metadata.readonly更改为drive.readonly以支持下载。我想念什么?

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) )

1 个答案:

答案 0 :(得分:0)

找到了。 Google的示例是缓存凭据文件(credentials.json)。当我最初运行该示例时,范围权限不是drive.readonly的权限,而是drive.metadata.readonly的权限。我认为当我更改它们时,该请求不再有效。

我删除并创建了凭据.json,然后重新运行该脚本(并在我的浏览器上重新批准了凭据请求),此操作成功。由于BytesIO并未真正写入磁盘,我还最终使用以下命令存储了数据。

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