Невозможно выполнить несколько запросов подряд с помощью API google drive

У меня проблема с API google drive.

Я использую этот код для подключения к моему аккаунту 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_632139208221-9tetq1fkkbud9ucmcq0bl3k4e3centem.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)
Вернуться на верх