Skip to content

Commit 7af949f

Browse files
committed
Add HttpAccessTokenRefreshError
This adds an exception class including the HTTP status for access token refresh requests made over HTTP. Fixes googleapis#309
1 parent 29347a9 commit 7af949f

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

oauth2client/client.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ class AccessTokenRefreshError(Error):
134134
"""Error trying to refresh an expired access token."""
135135

136136

137+
class HttpAccessTokenRefreshError(AccessTokenRefreshError):
138+
"""Error (with HTTP status) trying to refresh an expired access token."""
139+
def __init__(self, *args, **kwargs):
140+
super(HttpAccessTokenRefreshError, self).__init__(*args)
141+
self.status = kwargs.get('status')
142+
143+
137144
class TokenRevokeError(Error):
138145
"""Error trying to revoke a token."""
139146

@@ -830,7 +837,7 @@ def _refresh(self, http_request):
830837
refresh request.
831838
832839
Raises:
833-
AccessTokenRefreshError: When the refresh fails.
840+
HttpAccessTokenRefreshError: When the refresh fails.
834841
"""
835842
if not self.store:
836843
self._do_refresh_request(http_request)
@@ -858,7 +865,7 @@ def _do_refresh_request(self, http_request):
858865
refresh request.
859866
860867
Raises:
861-
AccessTokenRefreshError: When the refresh fails.
868+
HttpAccessTokenRefreshError: When the refresh fails.
862869
"""
863870
body = self._generate_refresh_request_body()
864871
headers = self._generate_refresh_request_headers()
@@ -898,7 +905,7 @@ def _do_refresh_request(self, http_request):
898905
self.store.locked_put(self)
899906
except (TypeError, ValueError):
900907
pass
901-
raise AccessTokenRefreshError(error_msg)
908+
raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
902909

903910
def _revoke(self, http_request):
904911
"""Revokes this credential and deletes the stored copy (if it exists).

oauth2client/gce.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from oauth2client._helpers import _from_bytes
2525
from oauth2client import util
26-
from oauth2client.client import AccessTokenRefreshError
26+
from oauth2client.client import HttpAccessTokenRefreshError
2727
from oauth2client.client import AssertionCredentials
2828

2929

@@ -80,7 +80,7 @@ def _refresh(self, http_request):
8080
the refresh request.
8181
8282
Raises:
83-
AccessTokenRefreshError: When the refresh fails.
83+
HttpAccessTokenRefreshError: When the refresh fails.
8484
"""
8585
query = '?scope=%s' % urllib.parse.quote(self.scope, '')
8686
uri = META.replace('{?scope}', query)
@@ -90,13 +90,14 @@ def _refresh(self, http_request):
9090
try:
9191
d = json.loads(content)
9292
except Exception as e:
93-
raise AccessTokenRefreshError(str(e))
93+
raise HttpAccessTokenRefreshError(str(e),
94+
status=response.status)
9495
self.access_token = d['accessToken']
9596
else:
9697
if response.status == 404:
9798
content += (' This can occur if a VM was created'
9899
' with no service account or scopes.')
99-
raise AccessTokenRefreshError(content)
100+
raise HttpAccessTokenRefreshError(content, status=response.status)
100101

101102
@property
102103
def serialization_data(self):

tests/test_client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import mock
3333
import six
34+
from six.moves import http_client
3435
from six.moves import urllib
3536

3637
from .http_mock import CacheMock
@@ -43,7 +44,7 @@
4344
from oauth2client import util as oauth2client_util
4445
from oauth2client.client import AccessTokenCredentials
4546
from oauth2client.client import AccessTokenCredentialsError
46-
from oauth2client.client import AccessTokenRefreshError
47+
from oauth2client.client import HttpAccessTokenRefreshError
4748
from oauth2client.client import ADC_HELP_MSG
4849
from oauth2client.client import AssertionCredentials
4950
from oauth2client.client import AUTHORIZED_USER
@@ -690,14 +691,15 @@ def test_token_refresh_failure(self):
690691
for status_code in REFRESH_STATUS_CODES:
691692
http = HttpMockSequence([
692693
({'status': status_code}, b''),
693-
({'status': '400'}, b'{"error":"access_denied"}'),
694+
({'status': http_client.BAD_REQUEST},
695+
b'{"error":"access_denied"}'),
694696
])
695697
http = self.credentials.authorize(http)
696698
try:
697699
http.request('http://example.com')
698-
self.fail('should raise AccessTokenRefreshError exception')
699-
except AccessTokenRefreshError:
700-
pass
700+
self.fail('should raise HttpAccessTokenRefreshError exception')
701+
except HttpAccessTokenRefreshError as e:
702+
self.assertEqual(http_client.BAD_REQUEST, e.status)
701703
self.assertTrue(self.credentials.access_token_expired)
702704
self.assertEqual(None, self.credentials.token_response)
703705

0 commit comments

Comments
 (0)