Skip to content

Commit

Permalink
add a backend for weibo.com
Browse files Browse the repository at this point in the history
  • Loading branch information
hepochen committed Jul 23, 2012
1 parent e1b7f85 commit dff5b97
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions social_auth/backends/contrib/weibo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#coding:utf8
#author:[email protected] https://github.com/hepochen
from django.utils import simplejson
from social_auth.backends import OAuthBackend, USERNAME, BaseOAuth2
from urllib import urlencode,urlopen

Weibo_SERVER = 'api.weibo.com'
Weibo_REQUEST_TOKEN_URL = 'https://%s/oauth2/request_token' % Weibo_SERVER
Weibo_ACCESS_TOKEN_URL = 'https://%s/oauth2/access_token' % Weibo_SERVER
Weibo_AUTHORIZATION_URL = 'https://%s/oauth2/authorize' % Weibo_SERVER


class WeiboBackend(OAuthBackend):
"""Weibo (of sina) OAuth authentication backend"""
name = 'weibo'
# Default extra data to store
EXTRA_DATA = [
('id', 'id'),
('profile_image_url','profile_image_url'),
('gender','gender')
]

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

def get_user_details(self, response):
"""Return user details from Douban"""
return {USERNAME: response["name"],
'first_name': response.get('screen_name','')}


class WeiboAuth(BaseOAuth2):
"""Douban OAuth authentication mechanism"""
AUTHORIZATION_URL = Weibo_AUTHORIZATION_URL
REQUEST_TOKEN_URL = Weibo_REQUEST_TOKEN_URL
ACCESS_TOKEN_URL = Weibo_ACCESS_TOKEN_URL
SERVER_URL = Weibo_SERVER
AUTH_BACKEND = WeiboBackend
SETTINGS_KEY_NAME = 'WEIBO_CLIENT_KEY'
SETTINGS_SECRET_NAME = 'WEIBO_CLIENT_SECRET'


def user_data(self, access_token, *args, **kwargs):
uid = args[0]['uid']
url = 'https://api.weibo.com/2/users/show.json'
data = {'access_token': access_token, 'uid': uid}
c = urlopen(url + '?' + urlencode(data)).read()
try:
return simplejson.loads(c)
except (ValueError, KeyError, IOError):
return None

# Backend definition
BACKENDS = {
'weibo': WeiboAuth,
}

0 comments on commit dff5b97

Please sign in to comment.