Token refresh to work with 'old' GData API
Contributed by crhyme.
Reviewed in http://codereview.appspot.com/6373049/.
diff --git a/oauth2client/client.py b/oauth2client/client.py
index e701f02..4e7ffcb 100644
--- a/oauth2client/client.py
+++ b/oauth2client/client.py
@@ -416,8 +416,9 @@
resp, content = request_orig(uri, method, body, headers,
redirections, connection_type)
- if resp.status == 401:
- logger.info('Refreshing due to a 401')
+ # Older API (GData) respond with 403
+ if resp.status in [401, 403]:
+ logger.info('Refreshing due to a %s' % str(resp.status))
self._refresh(request_orig)
self.apply(headers)
return request_orig(uri, method, body, headers,
diff --git a/tests/test_oauth2client.py b/tests/test_oauth2client.py
index faccccf..a618674 100644
--- a/tests/test_oauth2client.py
+++ b/tests/test_oauth2client.py
@@ -99,28 +99,32 @@
user_agent)
def test_token_refresh_success(self):
- http = HttpMockSequence([
- ({'status': '401'}, ''),
- ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'),
- ({'status': '200'}, 'echo_request_headers'),
- ])
- http = self.credentials.authorize(http)
- resp, content = http.request("http://example.com")
- self.assertEqual('Bearer 1/3w', content['Authorization'])
- self.assertFalse(self.credentials.access_token_expired)
+ # Older API (GData) respond with 403
+ for status_code in ['401', '403']:
+ http = HttpMockSequence([
+ ({'status': status_code}, ''),
+ ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'),
+ ({'status': '200'}, 'echo_request_headers'),
+ ])
+ http = self.credentials.authorize(http)
+ resp, content = http.request("http://example.com")
+ self.assertEqual('Bearer 1/3w', content['Authorization'])
+ self.assertFalse(self.credentials.access_token_expired)
def test_token_refresh_failure(self):
- http = HttpMockSequence([
- ({'status': '401'}, ''),
- ({'status': '400'}, '{"error":"access_denied"}'),
- ])
- http = self.credentials.authorize(http)
- try:
- http.request("http://example.com")
- self.fail("should raise AccessTokenRefreshError exception")
- except AccessTokenRefreshError:
- pass
- self.assertTrue(self.credentials.access_token_expired)
+ # Older API (GData) respond with 403
+ for status_code in ['401', '403']:
+ http = HttpMockSequence([
+ ({'status': status_code}, ''),
+ ({'status': '400'}, '{"error":"access_denied"}'),
+ ])
+ http = self.credentials.authorize(http)
+ try:
+ http.request("http://example.com")
+ self.fail("should raise AccessTokenRefreshError exception")
+ except AccessTokenRefreshError:
+ pass
+ self.assertTrue(self.credentials.access_token_expired)
def test_non_401_error_response(self):
http = HttpMockSequence([
@@ -148,17 +152,19 @@
self.credentials = AccessTokenCredentials(access_token, user_agent)
def test_token_refresh_success(self):
- http = HttpMockSequence([
- ({'status': '401'}, ''),
- ])
- http = self.credentials.authorize(http)
- try:
- resp, content = http.request("http://example.com")
- self.fail("should throw exception if token expires")
- except AccessTokenCredentialsError:
- pass
- except Exception:
- self.fail("should only throw AccessTokenCredentialsError")
+ # Older API (GData) respond with 403
+ for status_code in ['401', '403']:
+ http = HttpMockSequence([
+ ({'status': status_code}, ''),
+ ])
+ http = self.credentials.authorize(http)
+ try:
+ resp, content = http.request("http://example.com")
+ self.fail("should throw exception if token expires")
+ except AccessTokenCredentialsError:
+ pass
+ except Exception:
+ self.fail("should only throw AccessTokenCredentialsError")
def test_non_401_error_response(self):
http = HttpMockSequence([