Add retry loop for fetching authentication token if any 'Internal Failure' occurs (#368)
* Add retry loop for fetching authentication token if any 'Internal failure' occurs
This is to prevent errors while client tries to connect with server and
fails with 'Internal Failure' because it was unable to fetch OAuth2
token.
* Add retry loop for fetching authentication token if any 'Internal failure' occurs
This is to prevent errors while client tries to connect with server and
fails with 'Internal Failure' because it was unable to fetch OAuth2
token.
diff --git a/google/oauth2/_client.py b/google/oauth2/_client.py
index 5121a32..eac01b7 100644
--- a/google/oauth2/_client.py
+++ b/google/oauth2/_client.py
@@ -102,13 +102,23 @@
'content-type': _URLENCODED_CONTENT_TYPE,
}
- response = request(
- method='POST', url=token_uri, headers=headers, body=body)
+ retry = 0
+ # retry to fetch token for maximum of two times if any internal failure
+ # occurs.
+ while True:
+ response = request(
+ method='POST', url=token_uri, headers=headers, body=body)
+ response_body = response.data.decode('utf-8')
- response_body = response.data.decode('utf-8')
-
- if response.status != http_client.OK:
- _handle_error_response(response_body)
+ if response.status == http_client.OK:
+ break
+ else:
+ error_desc = json.loads(
+ response_body).get('error_description') or ''
+ if error_desc == 'internal_failure' and retry < 1:
+ retry += 1
+ continue
+ _handle_error_response(response_body)
response_data = json.loads(response_body)