This commit is contained in:
Mike Helmick 2013-10-26 19:04:36 -04:00
parent 6c2cac76a5
commit 638f75b93d
2 changed files with 22 additions and 1 deletions

View file

@ -9,6 +9,8 @@ class TwythonAuthTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.api = Twython(app_key, app_secret) self.api = Twython(app_key, app_secret)
self.bad_api = Twython('BAD_APP_KEY', 'BAD_APP_SECRET') self.bad_api = Twython('BAD_APP_KEY', 'BAD_APP_SECRET')
self.bad_api_invalid_tokens = Twython('BAD_APP_KEY', 'BAD_APP_SECRET',
'BAD_OT', 'BAD_OTS')
self.oauth2_api = Twython(app_key, app_secret, oauth_version=2) self.oauth2_api = Twython(app_key, app_secret, oauth_version=2)
self.oauth2_bad_api = Twython('BAD_APP_KEY', 'BAD_APP_SECRET', self.oauth2_bad_api = Twython('BAD_APP_KEY', 'BAD_APP_SECRET',
@ -31,6 +33,11 @@ class TwythonAuthTestCase(unittest.TestCase):
self.assertRaises(TwythonError, self.bad_api.get_authorized_tokens, self.assertRaises(TwythonError, self.bad_api.get_authorized_tokens,
'BAD_OAUTH_VERIFIER') 'BAD_OAUTH_VERIFIER')
def test_get_authorized_tokens_invalid_or_expired_tokens(self):
"""Test getting final token fails when invalid or expired tokens have been passed"""
self.assertRaises(TwythonError, self.bad_api_invalid_tokens.get_authorized_tokens,
'BAD_OAUTH_VERIFIER')
def test_get_authentication_tokens_raises_error_when_oauth2(self): def test_get_authentication_tokens_raises_error_when_oauth2(self):
"""Test when API is set for OAuth 2, get_authentication_tokens raises """Test when API is set for OAuth 2, get_authentication_tokens raises
a TwythonError""" a TwythonError"""

View file

@ -307,7 +307,21 @@ class Twython(EndpointsMixin, object):
if self.oauth_version != 1: if self.oauth_version != 1:
raise TwythonError('This method can only be called when your OAuth version is 1.0.') raise TwythonError('This method can only be called when your OAuth version is 1.0.')
response = self.client.get(self.access_token_url, params={'oauth_verifier': oauth_verifier}) response = self.client.get(self.access_token_url, params={'oauth_verifier': oauth_verifier}, headers={'Content-Type': 'application/json'})
if response.status_code == 401:
try:
try:
# try to get json
content = response.json()
except AttributeError: # pragma: no cover
# if unicode detected
content = json.loads(response.content)
except ValueError:
content = {}
raise TwythonError(content.get('error', 'Invalid / expired Token'), error_code=response.status_code)
authorized_tokens = dict(parse_qsl(response.content.decode('utf-8'))) authorized_tokens = dict(parse_qsl(response.content.decode('utf-8')))
if not authorized_tokens: if not authorized_tokens:
raise TwythonError('Unable to decode authorized tokens.') raise TwythonError('Unable to decode authorized tokens.')