Skip to content

Commit

Permalink
add access_type kwarg to OAuthHandler.get_authorization_url(), pass t…
Browse files Browse the repository at this point in the history
…hrough to Twitter's x_auth_access_type

Twitter's oauth/request_token endpoint supports a custom x_auth_access_type query parameter that may be 'read' or 'write'. This lets you request a read-only token for an app that has read/write permissions. Details: https://dev.twitter.com/docs/api/1/post/oauth/request_token

fixes tweepy#471
  • Loading branch information
snarfed committed Aug 12, 2014
1 parent 7e6052e commit ebbcfd6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 6 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ def testoauth(self):
s = api.update_status('test %i' % random.randint(0, 1000))
api.destroy_status(s.id)

def testaccesstype(self):
auth = OAuthHandler(oauth_consumer_key, oauth_consumer_secret)
auth_url = auth.get_authorization_url(access_type='read')
print('Please open: ' + auth_url)
answer = raw_input('Did Twitter only request read permissions? (y/n) ')
self.assertEqual('y', answer.lower())
8 changes: 5 additions & 3 deletions tweepy/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ def _get_oauth_url(self, endpoint):
def apply_auth(self):
return OAuth1(self.consumer_key, client_secret=self.consumer_secret, resource_owner_key=self.access_token, resource_owner_secret=self.access_token_secret)

def _get_request_token(self):
def _get_request_token(self, access_type = None):
try:
url = self._get_oauth_url('request_token')
if access_type:
url += '?x_auth_access_type=%s' % access_type
return self.oauth.fetch_request_token(url)
except Exception as e:
raise TweepError(e)
Expand All @@ -58,14 +60,14 @@ def set_access_token(self, key, secret):
self.access_token = key
self.access_token_secret = secret

def get_authorization_url(self, signin_with_twitter = False):
def get_authorization_url(self, signin_with_twitter = False, access_type = None):
"""Get the authorization URL to redirect the user"""
try:
if signin_with_twitter:
url = self._get_oauth_url('authenticate')
else:
url = self._get_oauth_url('authorize')
self.request_token = self._get_request_token()
self.request_token = self._get_request_token(access_type=access_type)
return self.oauth.authorization_url(url)
except Exception as e:
raise TweepError(e)
Expand Down

0 comments on commit ebbcfd6

Please sign in to comment.