1 from __future__ import print_function
6 from tweepy.error import TweepError
7 from tweepy.api import API
9 from requests_oauthlib import OAuth1Session, OAuth1
10 from requests.auth import AuthBase
11 from six.moves.urllib.parse import parse_qs
13 WARNING_MESSAGE = """Warning! Due to a Twitter API bug, signin_with_twitter
14 and access_type don't always play nice together. Details
15 https://dev.twitter.com/discussions/21281"""
18 class AuthHandler(object):
20 def apply_auth(self, url, method, headers, parameters):
21 """Apply authentication headers to request"""
22 raise NotImplementedError
24 def get_username(self):
25 """Return the username of the authenticated user"""
26 raise NotImplementedError
29 class OAuthHandler(AuthHandler):
30 """OAuth authentication handler"""
31 OAUTH_HOST = 'api.twitter.com'
32 OAUTH_ROOT = '/oauth/'
34 def __init__(self, consumer_key, consumer_secret, callback=None):
35 if type(consumer_key) == six.text_type:
36 consumer_key = consumer_key.encode('ascii')
38 if type(consumer_secret) == six.text_type:
39 consumer_secret = consumer_secret.encode('ascii')
41 self.consumer_key = consumer_key
42 self.consumer_secret = consumer_secret
43 self.access_token = None
44 self.access_token_secret = None
45 self.callback = callback
47 self.oauth = OAuth1Session(consumer_key,
48 client_secret=consumer_secret,
49 callback_uri=self.callback)
51 def _get_oauth_url(self, endpoint):
52 return 'https://' + self.OAUTH_HOST + self.OAUTH_ROOT + endpoint
55 return OAuth1(self.consumer_key,
56 client_secret=self.consumer_secret,
57 resource_owner_key=self.access_token,
58 resource_owner_secret=self.access_token_secret,
61 def _get_request_token(self, access_type=None):
63 url = self._get_oauth_url('request_token')
65 url += '?x_auth_access_type=%s' % access_type
66 return self.oauth.fetch_request_token(url)
67 except Exception as e:
70 def set_access_token(self, key, secret):
71 self.access_token = key
72 self.access_token_secret = secret
74 def get_authorization_url(self,
75 signin_with_twitter=False,
77 """Get the authorization URL to redirect the user"""
79 if signin_with_twitter:
80 url = self._get_oauth_url('authenticate')
82 logging.warning(WARNING_MESSAGE)
84 url = self._get_oauth_url('authorize')
85 self.request_token = self._get_request_token(access_type=access_type)
86 return self.oauth.authorization_url(url)
87 except Exception as e:
91 def get_access_token(self, verifier=None):
93 After user has authorized the request token, get access token
94 with user supplied verifier.
97 url = self._get_oauth_url('access_token')
98 self.oauth = OAuth1Session(self.consumer_key,
99 client_secret=self.consumer_secret,
100 resource_owner_key=self.request_token['oauth_token'],
101 resource_owner_secret=self.request_token['oauth_token_secret'],
102 verifier=verifier, callback_uri=self.callback)
103 resp = self.oauth.fetch_access_token(url)
104 self.access_token = resp['oauth_token']
105 self.access_token_secret = resp['oauth_token_secret']
106 return self.access_token, self.access_token_secret
107 except Exception as e:
110 def get_xauth_access_token(self, username, password):
112 Get an access token from an username and password combination.
113 In order to get this working you need to create an app at
114 http://twitter.com/apps, after that send a mail to api@twitter.com
115 and request activation of xAuth for it.
118 url = self._get_oauth_url('access_token')
119 oauth = OAuth1(self.consumer_key,
120 client_secret=self.consumer_secret)
121 r = requests.post(url=url,
123 headers={'x_auth_mode': 'client_auth',
124 'x_auth_username': username,
125 'x_auth_password': password})
128 credentials = parse_qs(r.content)
129 return credentials.get('oauth_token')[0], credentials.get('oauth_token_secret')[0]
130 except Exception as e:
133 def get_username(self):
134 if self.username is None:
136 user = api.verify_credentials()
138 self.username = user.screen_name
140 raise TweepError('Unable to get username,'
141 ' invalid oauth token!')
145 class OAuth2Bearer(AuthBase):
146 def __init__(self, bearer_token):
147 self.bearer_token = bearer_token
149 def __call__(self, request):
150 request.headers['Authorization'] = 'Bearer ' + self.bearer_token
154 class AppAuthHandler(AuthHandler):
155 """Application-only authentication handler"""
157 OAUTH_HOST = 'api.twitter.com'
158 OAUTH_ROOT = '/oauth2/'
160 def __init__(self, consumer_key, consumer_secret):
161 self.consumer_key = consumer_key
162 self.consumer_secret = consumer_secret
163 self._bearer_token = ''
165 resp = requests.post(self._get_oauth_url('token'),
166 auth=(self.consumer_key,
167 self.consumer_secret),
168 data={'grant_type': 'client_credentials'})
170 if data.get('token_type') != 'bearer':
171 raise TweepError('Expected token_type to equal "bearer", '
172 'but got %s instead' % data.get('token_type'))
174 self._bearer_token = data['access_token']
176 def _get_oauth_url(self, endpoint):
177 return 'https://' + self.OAUTH_HOST + self.OAUTH_ROOT + endpoint
179 def apply_auth(self):
180 return OAuth2Bearer(self._bearer_token)