@WyattWang 对的,你说的都对,需要对 https://github.com/bread-and-pepper/django-userena 进行修改,与新浪对接,看了你们站点,做得不错哈。与新浪对接关键就是在回调函数里。回调函数里采用Weibo客户端获取Id以及名字等信息,然后判断该用户是否在你的数据库里(可以使用id 加上一个type以标明他来自微博),底下的 _login_for_user 函数就是做这些事情。最后跳转就行了。
@secure_required
def weibo_callback(request):
"""
微博callback
"""
try:
code = request.GET.get('code', None)
client = _create_client()
r = client.request_access_token(code)
print 'access token: %s' % json.dumps(r)
access_token, expires_in, uid = r.access_token, r.expires_in, r.uid
client.set_access_token(access_token, expires_in)
u = client.users.show.get(uid = uid)
print 'got user: %s' % uid
request.session['access_token'] = access_token
request.session['expires_in'] = expires_in
# 进行登录(用户不存在,则创建)
new_user, is_new_user = _login_for_user(u, expires_in - r.expires, request)
if is_new_user == True:
redirect_to = reverse('userena_profile_edit',
kwargs = {'username': new_user.username})
else:
redirect_to = signin_redirect(
None, new_user)
except Exception, e:
return HttpResponse('连接新浪账号时出错:%s <p>返回首页:<a href="/">首页</a>' % e)
return HttpResponseRedirect(redirect_to)