无法使用 google 驱动器 API 连续发出多个请求

Unable to make multiple requests in a row using the google drive API

我的 google 驱动器 API 有问题。

我使用此代码连接到我的 google 帐户并获得服务:

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),它就可以工作。 如果我删除两个请求中的一个,它就可以工作...

我不明白我是怎么做到的..

非常感谢

我想我找到了解决方案:

def getCredentials():
# 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())
return creds


def getService(creds):

   service = build('drive', 'v3', credentials=creds)
   return service
service = getService(credentials)

和:

        http = google_auth_httplib2.AuthorizedHttp(credentials=GoogleDrive.credentials, http=httplib2.Http ())
                result = GoogleDrive.service.about().get(
        fields="storageQuota").execute(http=http)