Statistics
| Branch: | Tag: | Revision:

root / social / backends / strava.py @ a0a04c0a

History | View | Annotate | Download (1.6 kB)

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

    
7

    
8
class StravaOAuth(BaseOAuth2):
9
    name = 'strava'
10
    AUTHORIZATION_URL = 'https://www.strava.com/oauth/authorize'
11
    ACCESS_TOKEN_URL = 'https://www.strava.com/oauth/token'
12
    ACCESS_TOKEN_METHOD = 'POST'
13
    # Strava doesn't check for parameters in redirect_uri and directly appends
14
    # the auth parameters to it, ending with an URL like:
15
    # http://example.com/complete/strava?redirect_state=xxx?code=xxx&state=xxx
16
    # Check issue #259 for details.
17
    REDIRECT_STATE = False
18

    
19
    def get_user_id(self, details, response):
20
        return response['athlete']['id']
21

    
22
    def get_user_details(self, response):
23
        """Return user details from Strava account"""
24
        # because there is no usernames on strava
25
        username = response['athlete']['id']
26
        email = response['athlete'].get('email', '')
27
        fullname, first_name, last_name = self.get_user_names(
28
            first_name=response['athlete'].get('firstname', ''),
29
            last_name=response['athlete'].get('lastname', ''),
30
        )
31
        return {'username': str(username),
32
                'fullname': fullname,
33
                'first_name': first_name,
34
                'last_name': last_name,
35
                'email': email}
36

    
37
    def user_data(self, access_token, *args, **kwargs):
38
        """Loads user data from service"""
39
        return self.get_json('https://www.strava.com/api/v3/athlete',
40
                             params={'access_token': access_token})