root / social / backends / twitter.py @ a0a04c0a
History | View | Annotate | Download (1.4 kB)
1 |
"""
|
---|---|
2 |
Twitter OAuth1 backend, docs at:
|
3 |
http://psa.matiasaguirre.net/docs/backends/twitter.html
|
4 |
"""
|
5 |
from social.backends.oauth import BaseOAuth1 |
6 |
from social.exceptions import AuthCanceled |
7 |
|
8 |
|
9 |
class TwitterOAuth(BaseOAuth1): |
10 |
"""Twitter OAuth authentication backend"""
|
11 |
name = 'twitter'
|
12 |
EXTRA_DATA = [('id', 'id')] |
13 |
AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authenticate'
|
14 |
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
|
15 |
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
|
16 |
REDIRECT_STATE = True
|
17 |
|
18 |
def process_error(self, data): |
19 |
if 'denied' in data: |
20 |
raise AuthCanceled(self) |
21 |
else:
|
22 |
super(TwitterOAuth, self).process_error(data) |
23 |
|
24 |
def get_user_details(self, response): |
25 |
"""Return user details from Twitter account"""
|
26 |
fullname, first_name, last_name = self.get_user_names(response['name']) |
27 |
return {'username': response['screen_name'], |
28 |
'email': '', # not supplied |
29 |
'fullname': fullname,
|
30 |
'first_name': first_name,
|
31 |
'last_name': last_name}
|
32 |
|
33 |
def user_data(self, access_token, *args, **kwargs): |
34 |
"""Return user data provided"""
|
35 |
return self.get_json( |
36 |
'https://api.twitter.com/1.1/account/verify_credentials.json',
|
37 |
auth=self.oauth_auth(access_token)
|
38 |
) |