【发布时间】:2021-08-26 17:17:14
【问题描述】:
我的 google drive API 有问题。
我使用此代码连接到我的谷歌帐户并获得服务:
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
def getService():
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/drive']
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'code_secret_client_XXX.apps.googleusercontent.com.json',
SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('drive', 'v3', credentials=creds)
return service
效果很好,但是例如当我调用 2 次时:
result1 = GoogleDrive.service.files().list(
pageSize=1000, fields="nextPageToken, files(id, name)").execute()
result2 = GoogleDrive.service.about().get(
fields="storageQuota").execute()
我有这个错误:
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2633)
根据文档,Drive API 是建立在 Httplib2 之上的,它不是线程安全的。
我使用已弃用的oauth2client,这可能是问题吗?
如果我在请求之间添加time.sleep(1),它会起作用。
如果我删除这两个请求之一,它会起作用...
我不明白我怎么能做到这一点..
非常感谢
【问题讨论】:
标签: python django google-drive-api oauth2client