google-drive-sdk 导出每日限制未经身份验证的使用

Posted

技术标签:

【中文标题】google-drive-sdk 导出每日限制未经身份验证的使用【英文标题】:google-drive-sdk export Daily Limit unauthenticated use 【发布时间】:2018-07-14 11:43:13 【问题描述】:

我正在尝试根据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】:

找到了。 Google 的示例是缓存凭证文件 (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()

【讨论】:

以上是关于google-drive-sdk 导出每日限制未经身份验证的使用的主要内容,如果未能解决你的问题,请参考以下文章

在 Swift 中使用 Google 日历 API 时收到错误“超出未经身份验证使用的每日限制”

超出未经身份验证的使用的每日限制。继续使用需要注册错误

尝试在我的网络应用上使用 Google 和登录时,不断收到“超出未经身份验证使用的每日限制。继续使用需要注册”

继续获得“超出未经验证的使用的每日限制”。持续使用需要注册“尝试谷歌加上我的网络应用程序登录

在Swift中使用Google Calendar API时收到错误“超出未经身份验证的每日限制”

从Salesforce查看文件时,Google Drive API消息“超出未经身份验证的使用限制。”