From 920f433f8a5e5cab583127147e701584c4885f76 Mon Sep 17 00:00:00 2001 From: Mike Helmick Date: Sat, 28 Mar 2015 07:49:29 -0400 Subject: [PATCH 1/2] Fixes #371 --- twython/api.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/twython/api.py b/twython/api.py index d2d5c03..f324565 100644 --- a/twython/api.py +++ b/twython/api.py @@ -385,6 +385,36 @@ class Twython(EndpointsMixin, object): return authorized_tokens # pragma: no cover + def obtain_request_tokens(self, oauth_callback, x_auth_access_type='read'): + """Allows a Consumer application to obtain an OAuth Request Token to + request user authorization. + + :param oauth_callback: The value you specify here will be used as the + URL a user is redirected to should they approve your application’s + access to their account. Set this to oob for out-of-band pin mode. + This is also how you specify custom callbacks for use in desktop/mobile + applications. + :param x_auth_access_type: Overrides the access level an application + requests to a users account. Supported values are read or write. + This parameter is intended to allow a developer to register a + read/write application but also request read only access when + appropriate. + + :rtype: dict + """ + data = { + 'oauth_callback': oauth_callback, + 'x_auth_access_type': x_auth_access_type, + } + + response = self.client.post(self.request_token_url, data=data) + request_tokens = dict(parse_qsl(response.content.decode('utf-8'))) + + if not request_tokens: + raise TwythonError('Unable to decode request tokens.') + + return request_tokens # pragma: no cover + def obtain_access_token(self): """Returns an OAuth 2 access token to make OAuth 2 authenticated read-only calls. @@ -406,10 +436,12 @@ class Twython(EndpointsMixin, object): except AttributeError: content = json.loads(content) access_token = content['access_token'] + + return access_token except (KeyError, ValueError, requests.exceptions.RequestException): raise TwythonAuthError('Unable to obtain OAuth 2 access token.') - else: - return access_token + + return '' @staticmethod def construct_api_url(api_url, **params): From 1fa262628fdbdb6a5dfa904e8a1350761211f894 Mon Sep 17 00:00:00 2001 From: Mike Helmick Date: Sat, 28 Mar 2015 07:51:29 -0400 Subject: [PATCH 2/2] Optional parameter --- twython/api.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/twython/api.py b/twython/api.py index f324565..902dbe0 100644 --- a/twython/api.py +++ b/twython/api.py @@ -385,7 +385,7 @@ class Twython(EndpointsMixin, object): return authorized_tokens # pragma: no cover - def obtain_request_tokens(self, oauth_callback, x_auth_access_type='read'): + def obtain_request_tokens(self, oauth_callback, x_auth_access_type=''): """Allows a Consumer application to obtain an OAuth Request Token to request user authorization. @@ -404,9 +404,11 @@ class Twython(EndpointsMixin, object): """ data = { 'oauth_callback': oauth_callback, - 'x_auth_access_type': x_auth_access_type, } + if x_auth_access_type: + data['x_auth_access_type'] = x_auth_access_type + response = self.client.post(self.request_token_url, data=data) request_tokens = dict(parse_qsl(response.content.decode('utf-8')))