Google Drive API request timeout in python httplib2

840 views Asked by At

I have been using a google project with Google Drive API to create and update files in google drive, everything was working fine till yesterday. My python script was running fine too, but since couple of hours I am getting the following error:

lib/python3.9/site-packages/httplib2/__init__.py", line 1136, in connect
    sock.connect((self.host, self.port))
socket.timeout: timed out

This is the function I am using to connect with Google Drive API:

def Create_Service(client_secret_file, api_name, api_version, *scopes):
    print(client_secret_file, api_name, api_version, scopes, sep='-')
    CLIENT_SECRET_FILE = client_secret_file
    API_SERVICE_NAME = api_name
    API_VERSION = api_version
    SCOPES = [scope for scope in scopes[0]]
    print(SCOPES)

    cred = None

    pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}.pickle'
    # print(pickle_file)

    if os.path.exists(pickle_file):
        with open(pickle_file, 'rb') as token:
            cred = pickle.load(token)

    if not cred or not cred.valid:
        if cred and cred.expired and cred.refresh_token:
            cred.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
            cred = flow.run_local_server()

        with open(pickle_file, 'wb') as token:
            pickle.dump(cred, token)

    try:
        service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
        print(API_SERVICE_NAME, 'service created successfully')
        return service
    except Exception as e:
        print('Unable to connect.')
        print(e)
        return None

It's connecting to the Google Drive API but whenever my code is trying to do any action like read or write to a file or folder it's getting the socket.timeout: timed out. I have changed my credentials even created a new project in google dev console with fresh new credentials, re-authenticated the API with my google account to get a new token, but nothing seems to be working. I have checked the package dependency too with pip check and it returns there is no broken dependency.

# Edit 1:

My code to read file from Google Drive:

def __read_file_from_drive():
    CLIENT_SECRET_FILE = 'client_secrets.json'
    API_NAME = 'drive'
    API_VERSION = 'v3'
    SCOPES = ['https://www.googleapis.com/auth/drive']

    service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
    file = 'my_file_id'
    request = service.files().get_media(fileId=file)
    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fd=fh, request=request)
    
    done = False
    while not done:
        status, done = downloader.next_chunk()
        print('File downloaded...')

    fh.seek(0)
    return fh.read().decode('ascii')

Code to save file to Google Drive:

def __save_file_to_drive():
    CLIENT_SECRET_FILE = 'client_secrets.json'
    API_NAME = 'drive'
    API_VERSION = 'v3'
    SCOPES = ['https://www.googleapis.com/auth/drive']

    service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

    __folder_id = 'my_folder_id'
    __files = ['my_files_list']
    __mime_type = 'text/csv'

    for __file in __files:
        file_meta = {
            'name': __file,
            'parents': [__folder_id]
        }

        media = MediaFileUpload('./{0}'.format(__file), mimetype=__mime_type)
        service.files().create(
            body=file_meta,
            media_body=media,
            fields='id'
        ).execute()

The current error I am getting:

Traceback (most recent call last):
  File "~/PycharmProjects/project_name/google_drive_upload.py", line 75, in <module>
    returned = int(__read_file_from_drive())
  File "~/PycharmProjects/project_name/google_drive_upload.py", line 65, in __read_file_from_drive
    status, done = downloader.next_chunk()
  File "~/mambaforge/envs/project_name/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "~/mambaforge/envs/project_name/lib/python3.9/site-packages/googleapiclient/http.py", line 746, in next_chunk
    resp, content = _retry_request(
  File "~/mambaforge/envs/project_name/lib/python3.9/site-packages/googleapiclient/http.py", line 227, in _retry_request
    raise exception
  File "~/mambaforge/envs/project_name/lib/python3.9/site-packages/googleapiclient/http.py", line 196, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "~/mambaforge/envs/project_name/lib/python3.9/site-packages/google_auth_httplib2.py", line 218, in request
    response, content = self.http.request(
  File "~/mambaforge/envs/project_name/lib/python3.9/site-packages/httplib2/__init__.py", line 1708, in request
    (response, content) = self._request(
  File "~/mambaforge/envs/project_name/lib/python3.9/site-packages/httplib2/__init__.py", line 1424, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "~/mambaforge/envs/project_name/lib/python3.9/site-packages/httplib2/__init__.py", line 1346, in _conn_request
    conn.connect()
  File "~/mambaforge/envs/project_name/lib/python3.9/site-packages/httplib2/__init__.py", line 1136, in connect
    sock.connect((self.host, self.port))
socket.timeout: timed out
1

There are 1 answers

0
Rand Chen On

just leave a comment for someone came here.
i faced the same issue when i trying to access google drive api behind proxy, there didn't have a specification error message point you to proxy, so sometime it not easy to find the root cause.

if you already know it's a proxy issue, you can solve it quick and easy, set proxy from your IDE or CMD as below.

export http_proxy=http://{IP}:{port} 
export https_proxy=http://{IP}:{port}