+
+class OAuth2Bearer(AuthBase):
+ def __init__(self, bearer_token):
+ self.bearer_token = bearer_token
+
+ def __call__(self, request):
+ request.headers['Authorization'] = 'Bearer ' + self.bearer_token
+ return request
+
+
+class AppAuthHandler(AuthHandler):
+ """Application-only authentication handler"""
+
+ OAUTH_HOST = 'api.twitter.com'
+ OAUTH_ROOT = '/oauth2/'
+
+ def __init__(self, consumer_key, consumer_secret):
+ self.consumer_key = consumer_key
+ self.consumer_secret = consumer_secret
+ self._bearer_token = ''
+
+ resp = requests.post(self._get_oauth_url('token'),
+ auth=(self.consumer_key,
+ self.consumer_secret),
+ data={'grant_type': 'client_credentials'})
+ data = resp.json()
+ if data.get('token_type') != 'bearer':
+ raise TweepError('Expected token_type to equal "bearer", '
+ 'but got %s instead' % data.get('token_type'))
+
+ self._bearer_token = data['access_token']
+
+ def _get_oauth_url(self, endpoint):
+ return 'https://' + self.OAUTH_HOST + self.OAUTH_ROOT + endpoint
+
+ def apply_auth(self):
+ return OAuth2Bearer(self._bearer_token)