Skip to content
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 .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding//social/backends/spotify.py=utf-8
25 changes: 25 additions & 0 deletions docs/backends/beats.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Beats
=======

Beats supports OAuth 2.

- Register a new application at `Beats Music API`_, and follow the
instructions below.

OAuth2
------

Add the Beats OAuth2 backend to your settings page::

SOCIAL_AUTH_AUTHENTICATION_BACKENDS = (
...
'social.backends.beats.BeatsOAuth2',
...
)

- Fill ``App Key`` and ``App Secret`` values in the settings::

SOCIAL_AUTH_BEATS_OAUTH2_KEY = ''
SOCIAL_AUTH_BEATS_OAUTH2_SECRET = ''

.. _Beats Music API: https://developer.beatsmusic.com/docs
2 changes: 2 additions & 0 deletions docs/backends/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Social backends
angel
aol
appsfuel
beats
behance
belgium_eid
bitbucket
Expand Down Expand Up @@ -93,6 +94,7 @@ Social backends
shopify
skyrock
soundcloud
spotify
suse
stackoverflow
steam
Expand Down
25 changes: 25 additions & 0 deletions docs/backends/spotify.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Spotify
=======

Spotify supports OAuth 2.

- Register a new application at `Spotify Web API`_, and follow the
instructions below.

OAuth2
------

Add the Spotify OAuth2 backend to your settings page::

SOCIAL_AUTH_AUTHENTICATION_BACKENDS = (
...
'social.backends.spotify.SpotifyOAuth2',
...
)

- Fill ``App Key`` and ``App Secret`` values in the settings::

SOCIAL_AUTH_SPOTIFY_OAUTH2_KEY = ''
SOCIAL_AUTH_SPOTIFY_OAUTH2_SECRET = ''

.. _Spotify Web API: https://developer.spotify.com/spotify-web-api
4 changes: 4 additions & 0 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Several supported service by simple backends definition (easy to add new ones
or extend current one):

* Angel_ OAuth2
* Beats_ OAuth2
* Behance_ OAuth2
* Bitbucket_ OAuth1
* Box_ OAuth2
Expand Down Expand Up @@ -67,6 +68,7 @@ or extend current one):
* Shopify_ OAuth2
* Skyrock_ OAuth1
* Soundcloud_ OAuth2
* Spotify_ OAuth2
* ThisIsMyJam_ OAuth1
* Stackoverflow_ OAuth2
* Steam_ OpenId
Expand Down Expand Up @@ -111,6 +113,7 @@ section.
.. _OAuth: http://oauth.net/
.. _myOpenID: https://www.myopenid.com/
.. _Angel: https://angel.co
.. _Beats: https://www.beats.com
.. _Behance: https://www.behance.net
.. _Bitbucket: https://bitbucket.org
.. _Box: https://www.box.com
Expand Down Expand Up @@ -140,6 +143,7 @@ section.
.. _Shopify: http://shopify.com
.. _Skyrock: https://skyrock.com
.. _Soundcloud: https://soundcloud.com
.. _Spotify: https://www.spotify.com
.. _ThisIsMyJam: https://thisismyjam.com
.. _Stocktwits: https://stocktwits.com
.. _Stripe: https://stripe.com
Expand Down
2 changes: 2 additions & 0 deletions examples/django_example/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
'social.backends.angel.AngelOAuth2',
'social.backends.aol.AOLOpenId',
'social.backends.appsfuel.AppsfuelOAuth2',
'social.backends.beats.BeatsOAuth2',
'social.backends.behance.BehanceOAuth2',
'social.backends.belgiumeid.BelgiumEIDOpenId',
'social.backends.bitbucket.BitbucketOAuth',
Expand Down Expand Up @@ -168,6 +169,7 @@
'social.backends.runkeeper.RunKeeperOAuth2',
'social.backends.skyrock.SkyrockOAuth',
'social.backends.soundcloud.SoundcloudOAuth2',
'social.backends.spotify.SpotifyOAuth2',
'social.backends.stackoverflow.StackoverflowOAuth2',
'social.backends.steam.SteamOpenId',
'social.backends.stocktwits.StocktwitsOAuth2',
Expand Down
2 changes: 2 additions & 0 deletions examples/django_example/example/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<a href="{% url 'social:begin' 'angel' %}">Angel OAuth2</a> <br />
<a href="{% url 'social:begin' 'aol' %}">AOL OpenId</a> <br />
<a href="{% url 'social:begin' 'appsfuel' %}">Appsfuel OAuth2</a> <br />
<a href="{% url 'social:begin' 'beats' %}">Beats OAuth2</a> <br />
<a href="{% url 'social:begin' 'behance' %}">Behance OAuth2</a> <br />
<a href="{% url 'social:begin' 'belgiumeid' %}">BelgiumEID OpenId</a> <br />
<a href="{% url 'social:begin' 'bitbucket' %}">Bitbucket OAuth1</a> <br />
Expand Down Expand Up @@ -48,6 +49,7 @@
<a href="{% url 'social:begin' 'runkeeper' %}">Runkeeper OAuth2</a> <br />
<a href="{% url 'social:begin' 'skyrock' %}">Skyrock OAuth1</a> <br />
<a href="{% url 'social:begin' 'soundcloud' %}">Soundcloud OAuth2</a> <br />
<a href="{% url 'social:begin' 'spotify' %}">Spotify OAuth2</a> <br />
<a href="{% url 'social:begin' 'stackoverflow' %}">Stackoverflow OAuth2</a> <br />
<a href="{% url 'social:begin' 'steam' %}">Steam OpenId</a> <br />
<a href="{% url 'social:begin' 'stocktwits' %}">Stocktwits OAuth2</a> <br />
Expand Down
80 changes: 80 additions & 0 deletions social/backends/beats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
Beats backend, docs at:
https://developer.beatsmusic.com/docs
"""
from re import sub

import base64

from social.p3 import urlencode
from social.backends.oauth import BaseOAuth2

class BeatsOAuth2(BaseOAuth2):
name = 'beats'
SCOPE_SEPARATOR = ' '
ID_KEY = 'user_context'
AUTHORIZATION_URL = 'https://partner.api.beatsmusic.com/v1/oauth2/authorize'
ACCESS_TOKEN_URL = 'https://partner.api.beatsmusic.com/oauth2/token'
ACCESS_TOKEN_METHOD = 'POST'
REDIRECT_STATE = False
# STATE_PARAMETER = False
# EXTRA_DATA = [
# ('id', 'username'),
# ]

def get_user_id(self, details, response):
return response["result"][BeatsOAuth2.ID_KEY]

def auth_headers(self):
return {
'Authorization': 'Basic {0}'.format(base64.urlsafe_b64encode(
('{0}:{1}'.format(*self.get_key_and_secret()).encode())
))
}

def auth_complete(self, *args, **kwargs):
"""Completes loging process, must return user instance"""
self.process_error(self.data)
try:
response = self.request_access_token(
self.ACCESS_TOKEN_URL,
data=self.auth_complete_params(self.validate_state()),
headers=self.auth_headers(),
method=self.ACCESS_TOKEN_METHOD
)
except HTTPError as err:
if err.response.status_code == 400:
raise AuthCanceled(self)
else:
raise
except KeyError:
raise AuthUnknownError(self)
self.process_error(response)

# mashery wraps in jsonrpc
if response.get('jsonrpc', None):
response = response.get('result', None)

return self.do_auth(response['access_token'], response=response,
*args, **kwargs)

def get_user_details(self, response):
"""Return user details from Beats account"""
response = response["result"]
print response
fullname, first_name, last_name = self.get_user_names(
response.get('display_name')
)
return {'username': response.get('id'),
'email': response.get('email'),
'fullname': fullname,
'first_name': first_name,
'last_name': last_name}

def user_data(self, access_token, *args, **kwargs):
"""Loads user data from service"""

return self.get_json(
'https://partner.api.beatsmusic.com/v1/api/me',
headers={'Authorization': 'Bearer {0}'.format(access_token)}
)
1 change: 1 addition & 0 deletions social/backends/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ def auth_complete(self, *args, **kwargs):
except KeyError:
raise AuthUnknownError(self)
self.process_error(response)

return self.do_auth(response['access_token'], response=response,
*args, **kwargs)

Expand Down
50 changes: 50 additions & 0 deletions social/backends/spotify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Spotify backend, docs at:
https://developer.spotify.com/spotify-web-api/
https://developer.spotify.com/spotify-web-api/authorization-guide/
"""
from re import sub

import base64

from social.p3 import urlencode
from social.backends.oauth import BaseOAuth2

class SpotifyOAuth2(BaseOAuth2):
name = 'spotify'
SCOPE_SEPARATOR = ' '
ID_KEY = 'id'
AUTHORIZATION_URL = 'https://accounts.spotify.com/authorize'
ACCESS_TOKEN_URL = 'https://accounts.spotify.com/api/token'
ACCESS_TOKEN_METHOD = 'POST'
REDIRECT_STATE = False
STATE_PARAMETER = False
# EXTRA_DATA = [
# ('id', 'username'),
# ]

def auth_headers(self):
return {
'Authorization': 'Basic {0}'.format(base64.urlsafe_b64encode(
('{0}:{1}'.format(*self.get_key_and_secret()).encode())
))
}

def get_user_details(self, response):
"""Return user details from Spotify account"""
fullname, first_name, last_name = self.get_user_names(
response.get('display_name')
)
return {'username': response.get('id'),
'email': response.get('email'),
'fullname': fullname,
'first_name': first_name,
'last_name': last_name}

def user_data(self, access_token, *args, **kwargs):
"""Loads user data from service"""

return self.get_json(
'https://api.spotify.com/v1/me',
headers={'Authorization': 'Bearer {0}'.format(access_token)}
)