Exemple #1
0
def display_userinfo(request):
    if request.session.has_key('oauth_data'):
        data = request.session['oauth_data']
        access_token = data['access_token']
        openid = data['openid']
        wx_usermanager = WeiXinUserManager()
        if not wx_usermanager.check_auth(access_token, openid):
            data = wx_usermanager.refresh_oauth2_access_token(
                                    data['refresh_token'])
            request.session['oauth_data'] = data
            access_token = data['access_token']
            openid = data['openid']
        data = wx_usermanager.get_userinfo(access_token, openid)
        data = utils.trans_userinfo(data)
        return render_to_response('wechat/display_userinfo.html',
                                  {'userinfo': data})
    code = request.GET.get('code')
    state = request.GET.get('state')
    if code is None:
        # 授权失败
        return render_to_response('wechat/error.html')
    wx_usermanager = WeiXinUserManager()
    data = wx_usermanager.get_oauth2_access_token(code)
    request.session['oauth_data'] = data
    return redirect(reverse('wechat:userinfo'))
Exemple #2
0
def home(request):
    if request.session.has_key('oauth_data'):
        data = request.session['oauth_data']
        access_token = data['access_token']
        openid = data['openid']
        wx_usermanager = WeiXinUserManager()
        if not wx_usermanager.check_auth(access_token, openid):
            data = wx_usermanager.refresh_oauth2_access_token(
                                    data['refresh_token'])
            request.session['oauth_data'] = data
            access_token = data['access_token']
            openid = data['openid']
        data = wx_usermanager.get_userinfo(access_token, openid)
        data = utils.trans_userinfo(data)
        return render_to_response('wechat/display_userinfo.html',
                                  {'userinfo': data})
    redirect_uri = urllib.quote_plus('http://slience.xyz' + reverse('wechat:userinfo'))
    context = {'appid': conf.get('appid'),
               'redirect_uri': redirect_uri,
               'scope': 'snsapi_userinfo',
               'state': 'userinfo'}
    return render_to_response('wechat/index.html', context)