Esempio n. 1
0
def login_check(request):
    """用户成功登录授权后,会回调此方法,获取access_token,完成授权"""
    code = request.GET.get('code')
    login_backurl = request.build_absolute_uri('/login_check')
    client = sinat_helper.get_oauth(login_backurl)
    logging.info('code: %s' % code)
    r = client.client.request_access_token(code)
    access_token = r.access_token
    expires_in = r.expires_in
    logging.info("r: %s" % r)
    # save access_token
    request.session['oauth_access_token'] = access_token
    request.session['oauth_request_expires'] = expires_in
    request.session['oauth_uid'] = r.uid
    client.client.set_access_token(access_token, expires_in)

    # 添加用户
    sina_user = client.get_user(r.uid)
    domain = 'http://weibo.com/%s' % (sina_user.id,)
    if not models.User.exists(str(sina_user.id)):
        models.User.add_user(str(sina_user.id), sina_user.screen_name,
                domain, sina_user.profile_image_url)
    else:
        models.User.update(str(sina_user.id), sina_user.screen_name,
                domain, sina_user.profile_image_url)

    back_to_url = request.session.get('login_back_to_url', '/')
    return HttpResponseRedirect(back_to_url)
Esempio n. 2
0
def weibo_login(request):
    login_backurl = request.build_absolute_uri('/login_check')
    client = sinat_helper.get_oauth(login_backurl)
    url = client.get_authorize_url()
    template = loader.get_template('templates/weibo_login.html')
    context = Context({
        'url': url,
        })

    context.update(RequestContext(request))
    return HttpResponse(template.render(context))
Esempio n. 3
0
def login(request):
    back_to_url = request_helper.get_referer_url(request)
    login_backurl = request.build_absolute_uri('/login_check')
    logging.debug(login_backurl)
    client = sinat_helper.get_oauth(login_backurl)
    if 'oauth_request_expires' not in request.session:
        request.session['login_back_to_url'] = back_to_url
        return HttpResponseRedirect('/weibo_login')
    else:
        access_token = request.session['oauth_access_token']
        expires_in = request.session['oauth_request_expires']
        client.client.set_access_token(access_token, expires_in)
        return HttpResponseRedirect(back_to_url)
Esempio n. 4
0
 def wrapper(self, *args, **kwargs):
     if 'oauth_access_token' not in self.session:
         return HttpResponseRedirect('/login')
     else:
         access_token = self.session['oauth_access_token']
         expires_in = self.session['oauth_request_expires']
         uid = self.session['oauth_uid']
         logging.info('access_token: %s, expires_in: %s' % (access_token,
             expires_in))
         logging.debug('access_token: %s, expires_in: %s' % (access_token,
             expires_in))
         login_backurl = self.build_absolute_uri('/login_check')
         self.auth_client = sinat_helper.get_oauth(login_backurl,
                 access_token, expires_in)
         self.uid = uid
         self.user = User.get_user_by_id(uid)
         return method(self, *args, **kwargs)