2

I have project that involves taking a picture every one hour with a Raspberry and saving it to Google Drive. I'm using the google-api-python-client to connect to a Google Drive and apscheduler to automate the process in the background. Around 80% of the time it works without any issues, but around 20% of the time I'm getting Timeout errors.

I added 5 retries to the service object, and extended the Timetout waiting period to 3 minutes, saw no difference. Right now I stopped the automated process and tried to connect manually using the open() function detailed below and I'm getting the same timeout problem no matter what I do. My internet is working fine without issues at the moment. What I find strange is that it seems to work without any issues for hours until it suddenly does not for a while and then it works again. Is something expiring? Is there some limited resource I'm consuming without realizing? I checked the documentation and I'm nowhere near consuming Google Drive's API Quota.

I would really appreciate some help here.

import os
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from error_logger import ErrorLogger
from ssl import SSLCertVerificationError
import socket

SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = "API_KEYS/my_credentials.json"

credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
connected = False
timeout_in_sec = 60*3

def open():
    try:
        global connected
        socket.setdefaulttimeout(timeout_in_sec)
        service =  build('drive', 'v3', credentials=credentials)
        service.files().list(pageSize=1, fields="files(id, name)").execute()
        socket.setdefaulttimeout(None)
        connected = True
        return service
    except Exception as e:
        ErrorLogger.log(e)

def close(service):
    global connected
    connected = False
    service.close()

service = open()
close(service)

Error message:

2025-03-29 15:00:02,425 - my_application - ERROR - timed out Traceback (most recent call last): File "/home/pablo/Desktop/estacion_captura/gservice.py", line 38, in open service.files().list(pageSize=1, fields="files(id, name)").execute() File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper return wrapped(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/googleapiclient/http.py", line 923, in execute resp, content = _retry_request( ^^^^^^^^^^^^^^^ File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/googleapiclient/http.py", line 222, in _retry_request raise exception File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/googleapiclient/http.py", line 191, in _retry_request resp, content = http.request(uri, method, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/google_auth_httplib2.py", line 209, in request self.credentials.before_request(self._request, method, uri, request_headers) File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/google/auth/credentials.py", line 156, in before_request self.refresh(request) File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/google/oauth2/service_account.py", line 438, in refresh access_token, expiry, _ = _client.jwt_grant( ^^^^^^^^^^^^^^^^^^ File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/google/oauth2/_client.py", line 312, in jwt_grant response_data = _token_endpoint_request( ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/google/oauth2/_client.py", line 272, in _token_endpoint_request response_status_ok, response_data, retryable_error = _token_endpoint_request_no_throw( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/google/oauth2/_client.py", line 219, in _token_endpoint_request_no_throw request_succeeded, response_data, retryable_error = _perform_request() ^^^^^^^^^^^^^^^^^^ File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/google/oauth2/_client.py", line 195, in _perform_request response = request( ^^^^^^^^ File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/google_auth_httplib2.py", line 119, in call response, data = self.http.request( ^^^^^^^^^^^^^^^^^^ File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/httplib2/init.py", line 1724, in request (response, content) = self._request( ^^^^^^^^^^^^^^ File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/httplib2/init.py", line 1444, in _request (response, content) = self._conn_request(conn, request_uri, method, body, headers) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/httplib2/init.py", line 1366, in _conn_request conn.connect() File "/home/pablo/Desktop/estacion_captura/venvs/estacion_captura2/lib/python3.11/site-packages/httplib2/init.py", line 1156, in connect sock.connect((self.host, self.port)) TimeoutError: timed out

1 Answer 1

0

I solved the problem by using Cloudflare's DNS in my Ubuntu desktop. I followed the steps mentioned in this article, under "Change DNS Nameserver via Network Settings" section. After this, the connection timeout issues disappeared.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you give more information about what lead you to that article? How did you realized the issue connecting to the google api was originated by a DNS issue? I'll try this solution soon, but I would really like to understand why this error happened in the first place. Thanks in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.