Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
Mixcloud OAuth2 support | ||
""" | ||
from urllib import urlencode | ||
from urllib2 import Request, urlopen | ||
from oauth2 import Request as OAuthRequest | ||
from django.utils import simplejson | ||
from social_auth.utils import setting | ||
from social_auth.backends import OpenIdAuth, ConsumerBasedOAuth, BaseOAuth2, \ | ||
OAuthBackend, OpenIDBackend, USERNAME | ||
from social_auth.backends.exceptions import AuthFailed | ||
from pprint import pprint | ||
|
||
MIXCLOUD_PROFILE_URL = 'https://api.mixcloud.com/me/' | ||
|
||
class MixcloudBackend(OAuthBackend): | ||
name = 'mixcloud' | ||
|
||
def get_user_id(self, details, response): | ||
return response['username'] | ||
|
||
def get_user_details(self, response): | ||
return {USERNAME: response['username'], | ||
'email': None, | ||
'fullname': response['name'], | ||
'first_name': None, | ||
'last_name': None } | ||
|
||
class MixcloudOAuth2(BaseOAuth2): | ||
AUTH_BACKEND = MixcloudBackend | ||
AUTHORIZATION_URL = 'https://www.mixcloud.com/oauth/authorize' | ||
ACCESS_TOKEN_URL = 'https://www.mixcloud.com/oauth/access_token' | ||
SETTINGS_KEY_NAME = 'MIXCLOUD_CLIENT_ID' | ||
SETTINGS_SECRET_NAME = 'MIXCLOUD_CLIENT_SECRET' | ||
|
||
def user_data(self, access_token, *args, **kwargs): | ||
return mixcloud_profile(access_token) | ||
|
||
def mixcloud_profile(access_token): | ||
data = {'access_token': access_token, 'alt': 'json'} | ||
request = Request(MIXCLOUD_PROFILE_URL + '?' + urlencode(data)) | ||
try: | ||
return simplejson.loads(urlopen(request).read()) | ||
except (ValueError, KeyError, IOError): | ||
return None | ||
|
||
BACKENDS = { | ||
'mixcloud': MixcloudOAuth2, | ||
} |