diff --git a/google/oauth2/_client.py b/google/oauth2/_client.py index 996f9b7e2..f92b0970a 100644 --- a/google/oauth2/_client.py +++ b/google/oauth2/_client.py @@ -104,18 +104,21 @@ def _token_endpoint_request(request, token_uri, body): while True: response = request(method="POST", url=token_uri, headers=headers, body=body) response_body = response.data.decode("utf-8") + response_data = json.loads(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: + error_desc = response_data.get("error_description") or "" + error_code = response_data.get("error") or "" + if ( + any(e == "internal_failure" for e in (error_code, error_desc)) + and retry < 1 + ): retry += 1 continue _handle_error_response(response_body) - response_data = json.loads(response_body) - return response_data diff --git a/tests/oauth2/test__client.py b/tests/oauth2/test__client.py index c415a1f44..745a4b675 100644 --- a/tests/oauth2/test__client.py +++ b/tests/oauth2/test__client.py @@ -123,6 +123,15 @@ def test__token_endpoint_request_internal_failure_error(): {"error": "internal_failure", "error_description": "internal_failure"}, ) + request = make_request( + {"error": "internal_failure"}, status=http_client.BAD_REQUEST + ) + + with pytest.raises(exceptions.RefreshError): + _client._token_endpoint_request( + request, "http://example.com", {"error": "internal_failure"} + ) + def verify_request_params(request, params): request_body = request.call_args[1]["body"]