示例#1
0
def callback(request):
    code = request.GET.get('code')
    client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
    r = client.request_access_token(code)
    access_token = r.access_token
    expires_in = r.expires_in
    client.set_access_token(access_token, expires_in)
    sina_info = client.get.users__show(uid=r.uid)
    username="******"+r.uid
    sinauser = User.objects.filter(username=username)
    if request.user.is_authenticated() and len(sinauser) > 0 and sinauser[0] != request.user:
            return HttpResponse("错误!每个微博帐号只能绑定一个帐号!")
    if len(sinauser) > 0 :
        #处理已绑定的存在的用户
        login_user = sinauser[0]

        login_user.backend='django.contrib.auth.backends.ModelBackend'
        login(request, login_user)
        profile = login_user.get_profile()
        profile.sina_code = access_token
        profile.sina_expire = expires_in
        profile.save()
        return HttpResponseRedirect("/")
    if request.user.is_authenticated() :
        #这个是处理绑定微薄账号的
        user = request.user
        user.username = username
        user.save()
        profile = user.get_profile()
        profile.sina_code = access_token
        profile.sina_expire = expires_in
        profile.save()
        return HttpResponseRedirect(reverse('/'))

    user = create_sina_user(r.uid)
    user.backend='django.contrib.auth.backends.ModelBackend'
    login(request, user)
    name = sina_info['name']
    t_profile = Profile.objects.filter(name=name)#判断昵称可用
    profile , _= Profile.objects.get_or_create(user=user)
    form = ProfileEditForm(instance=profile)
    profile.name = name
    if t_profile:
        profile.name=username
    profile.sina_code=access_token
    profile.sina_expire = expires_in
    profile.save()
    return HttpResponseRedirect(reverse('/'))
示例#2
0
def sina_auth_redirect(request):
    client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
    url = client.get_authorize_url()
    return HttpResponseRedirect(url)