google-api-python-client icon indicating copy to clipboard operation
google-api-python-client copied to clipboard

num_retries is not covering authentication part "oauth2.googleapis.com"

Open NicolaSpreafico opened this issue 2 years ago • 0 comments

Hello, I have this simple code to use the library

credentials = ...
service = discovery.build('gmail', 'v1', credentials=credentials)

Then I execute an API Call

response = service.users().settings().sendAs().list(userId="me").execute(num_retries)

If there are DNS temporary errors, I found out that if the connection issue is related to the execution of the actual request, the retry actually kicks-in

[2023-05-31, 20:44:49 UTC] {http.py:163} WARNING - Sleeping 0.13 seconds before retry 1 of 10 for request: GET https://gmail.googleapis.com/gmail/v1/users/me/settings/delegates?alt=json, after Unable to find the server at gmail.googleapis.com

[2023-05-31, 20:45:11 UTC] {http.py:163} WARNING - Sleeping 1.45 seconds before retry 1 of 1 for request: GET https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest, after Unable to find the server at www.googleapis.com

[2023-06-01, 01:24:50 UTC] {http.py:163} WARNING - Sleeping 0.11 seconds before retry 1 of 1 for request: GET https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest, after Unable to find the server at www.googleapis.com

But if the DNS error is triggered from the (what I think is) authentication part, probably when it tries to create the first token or renew it, the error is blocking and the request ends without retries

gmail_service.users().settings().forwardingAddresses().list(userId="me").execute(num_retries=10)
  File "/opt/python3.8/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/opt/python3.8/lib/python3.8/site-packages/googleapiclient/http.py", line 900, in execute
    resp, content = _retry_request(
  File "/opt/python3.8/lib/python3.8/site-packages/googleapiclient/http.py", line 177, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "/opt/python3.8/lib/python3.8/site-packages/google_auth_httplib2.py", line 209, in request
    self.credentials.before_request(self._request, method, uri, request_headers)
  File "/opt/python3.8/lib/python3.8/site-packages/google/auth/credentials.py", line 134, in before_request
    self.refresh(request)
  File "/opt/python3.8/lib/python3.8/site-packages/google/oauth2/service_account.py", line 429, in refresh
    access_token, expiry, _ = _client.jwt_grant(
  File "/opt/python3.8/lib/python3.8/site-packages/google/oauth2/_client.py", line 289, in jwt_grant
    response_data = _token_endpoint_request(
  File "/opt/python3.8/lib/python3.8/site-packages/google/oauth2/_client.py", line 250, in _token_endpoint_request
    response_status_ok, response_data, retryable_error = _token_endpoint_request_no_throw(
  File "/opt/python3.8/lib/python3.8/site-packages/google/oauth2/_client.py", line 199, in _token_endpoint_request_no_throw
    request_succeeded, response_data, retryable_error = _perform_request()
  File "/opt/python3.8/lib/python3.8/site-packages/google/oauth2/_client.py", line 175, in _perform_request
    response = request(
  File "/opt/python3.8/lib/python3.8/site-packages/google_auth_httplib2.py", line 126, in __call__
    raise exceptions.TransportError(exc)
	
google.auth.exceptions.TransportError: Unable to find the server at oauth2.googleapis.com

Is there a way to apply the num_retries parameters also to the authentication part?

The why sometimes the DNS fails to resolve the host is under investigation with Google support, and right now I created an outer loop that when the request fails tries again (something like a custom implementation for the retry backoff).

NicolaSpreafico avatar Jun 01 '23 06:06 NicolaSpreafico