Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for strava #217

Merged
merged 3 commits into from
Mar 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ or current ones extended):
* Stackoverflow_ OAuth2
* Steam_ OpenId
* Stocktwits_ OAuth2
* Strava_ OAuth2
* Stripe_ OAuth2
* Taobao_ OAuth2 http://open.taobao.com/doc/detail.htm?id=118
* ThisIsMyJam_ OAuth1 https://www.thisismyjam.com/developers/authentication
Expand Down Expand Up @@ -239,6 +240,7 @@ check `django-social-auth LICENSE`_ for details:
.. _Skyrock: https://skyrock.com
.. _Soundcloud: https://soundcloud.com
.. _Stocktwits: https://stocktwits.com
.. _Strava: http://strava.com
.. _Stripe: https://stripe.com
.. _Taobao: http://open.taobao.com/doc/detail.htm?id=118
.. _Tripit: https://www.tripit.com
Expand Down
17 changes: 17 additions & 0 deletions docs/backends/strava.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Strava
=========

Strava uses OAuth v2 for Authentication.

- Register a new application at the `Strava API`_, and

- fill ``Client ID`` and ``Client Secret`` from strava.com values in the settings::

SOCIAL_AUTH_STRAVA_KEY = ''
SOCIAL_AUTH_STRAVA_SECRET = ''

- extra scopes can be defined by using::

SOCIAL_AUTH_INSTAGRAM_AUTH_EXTRA_ARGUMENTS = {'scope': 'likes comments relationships'}

.. _Strava API: https://www.strava.com/settings/api
1 change: 1 addition & 0 deletions examples/django_me_example/example/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<a href="{% url 'social:begin' "yahoo" %}">Yahoo OpenId</a> <br />
<a href="{% url 'social:begin' "yahoo-oauth" %}">Yahoo OAuth</a> <br />
<a href="{% url 'social:begin' "stripe" %}">Stripe OAuth2</a> <br />
<a href="{% url 'social:begin' "strava" %}">Strava OAuth2</a> <br />
<a href="{% url 'social:begin' "facebook" %}">Facebook OAuth2</a> <br />
<a href="{% url 'social:begin' "facebook-app" %}">Facebook App</a> <br />
<a href="{% url 'social:begin' "angel" %}">Angel OAuth2</a> <br />
Expand Down
29 changes: 29 additions & 0 deletions social/backends/strava.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Strava OAuth2 backend, docs at:
http://psa.matiasaguirre.net/docs/backends/strava.html
"""
from social.backends.oauth import BaseOAuth2


class StravaOAuth(BaseOAuth2):
name = 'strava'
AUTHORIZATION_URL = 'https://www.strava.com/oauth/authorize'
ACCESS_TOKEN_URL = 'https://www.strava.com/oauth/token'
ACCESS_TOKEN_METHOD = 'POST'

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

def get_user_details(self, response):
"""Return user details from Strava account"""
username = response['athlete']['id'] # because there is no usernames on strava
first_name = response['athlete'].get('first_name', '')
email = response['athlete'].get('email', '')
return {'username': username,
'first_name': first_name,
'email': email}

def user_data(self, access_token, *args, **kwargs):
"""Loads user data from service"""
return self.get_json('https://www.strava.com/api/v3/athlete',
params={'access_token': access_token})
62 changes: 62 additions & 0 deletions social/tests/backends/test_strava.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import json

from social.tests.backends.oauth import OAuth2Test


class StravaOAuthTest(OAuth2Test):
backend_path = 'social.backends.strava.StravaOAuth'
user_data_url = 'https://www.strava.com/api/v3/athlete'
expected_username = 227615
access_token_body = json.dumps({
"access_token": "83ebeabdec09f6670863766f792ead24d61fe3f9",
"athlete": {
"id": 227615,
"resource_state": 3,
"firstname": "John",
"lastname": "Applestrava",
"profile_medium": "http://pics.com/227615/medium.jpg",
"profile": "http://pics.com/227615/large.jpg",
"city": "San Francisco",
"state": "California",
"country": "United States",
"sex": "M",
"friend": null,
"follower": null,
"premium": true,
"created_at": "2008-01-01T17:44:00Z",
"updated_at": "2013-09-04T20:00:50Z",
"follower_count": 273,
"friend_count": 19,
"mutual_friend_count": 0,
"date_preference": "%m/%d/%Y",
"measurement_preference": "feet",
"email": "john@applestrava.com",
"clubs": [ ],
"bikes": [ ],
"shoes": [ ]
}
})
user_data_body = json.dumps({
"id": 227615,
"resource_state": 2,
"firstname": "John",
"lastname": "Applestrava",
"profile_medium": "http://pics.com/227615/medium.jpg",
"profile": "http://pics.com/227615/large.jpg",
"city": "San Francisco",
"state": "CA",
"country": "United States",
"sex": "M",
"friend": null,
"follower": "accepted",
"premium": true,
"created_at": "2011-03-19T21:59:57Z",
"updated_at": "2013-09-05T16:46:54Z",
"approve_followers": false
})

def test_login(self):
self.do_login()

def test_partial_pipeline(self):
self.do_partial_pipeline()