Statistics
| Branch: | Tag: | Revision:

root / social / backends / amazon.py @ a0a04c0a

History | View | Annotate | Download (1.5 kB)

1
"""
2
Amazon OAuth2 backend, docs at:
3
    http://psa.matiasaguirre.net/docs/backends/amazon.html
4
"""
5
from social.backends.oauth import BaseOAuth2
6

    
7

    
8
class AmazonOAuth2(BaseOAuth2):
9
    name = 'amazon'
10
    ID_KEY = 'user_id'
11
    AUTHORIZATION_URL = 'http://www.amazon.com/ap/oa'
12
    ACCESS_TOKEN_URL = 'https://api.amazon.com/auth/o2/token'
13
    DEFAULT_SCOPE = ['profile']
14
    REDIRECT_STATE = False
15
    ACCESS_TOKEN_METHOD = 'POST'
16
    EXTRA_DATA = [
17
        ('refresh_token', 'refresh_token', True),
18
        ('user_id', 'user_id'),
19
        ('postal_code', 'postal_code')
20
    ]
21

    
22
    def get_user_details(self, response):
23
        """Return user details from amazon account"""
24
        name = response.get('name') or ''
25
        fullname, first_name, last_name = self.get_user_names(name)
26
        return {'username': name,
27
                'email': response.get('email'),
28
                'fullname': fullname,
29
                'first_name': first_name,
30
                'last_name': last_name}
31

    
32
    def user_data(self, access_token, *args, **kwargs):
33
        """Grab user profile information from amazon."""
34
        response = self.get_json('https://www.amazon.com/ap/user/profile',
35
                                 params={'access_token': access_token})
36
        if 'Profile' in response:
37
            response = {
38
                'user_id': response['Profile']['CustomerId'],
39
                'name': response['Profile']['Name'],
40
                'email': response['Profile']['PrimaryEmail']
41
            }
42
        return response