Search code examples
pythondjangogoogle-drive-apioauth2client

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


I have a problem with my google drive API.

I use this code to connect to my google account and get service :

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

It works perfectly, but when I call 2 times for example :

result1 = GoogleDrive.service.files().list(
        pageSize=1000, fields="nextPageToken, files(id, name)").execute()

result2 = GoogleDrive.service.about().get(
        fields="storageQuota").execute()

I have this error :

ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2633)

According to the documentation the Drive API is built on top of Httplib2 which is not thread safe.

I use oauth2client which is deprecated, could that be the problem?

If I add a time.sleep(1) between my requests, it works. if I remove one of the two requests, it works...

I don't understand how I can achieve that..

Thanks a lot


Solution

  • I think I found a solution :

    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)
    

    and :

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