【问题标题】:google-drive-sdk export Daily Limit unauthenticated usegoogle-drive-sdk 导出每日限制未经身份验证的使用
【发布时间】: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) )

【问题讨论】:

    标签: python google-drive-api


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2016-05-29
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 2017-09-06
      • 2023-04-06
      相关资源
      最近更新 更多