Пример #1
0
def authorization(request):
    """
    获取用户信息,登录,跳转
    :param request:
    :return:
    """
    """
    https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6fe7f0568b75d925&redirect_uri=http://www.yinzishao.cn/authorization&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect
    """
    code = request.GET.get('code')
    auth_redirect_uri = DOMAIN + "authorization"
    api = WeixinAPI(appid=APP_ID,
	    app_secret=APP_SECRET,
	    redirect_uri=auth_redirect_uri)
    auth_info = api.exchange_code_for_access_token(code=code)
    api = WeixinAPI(access_token=auth_info['access_token'])
    resp = api.user(openid=auth_info['openid'])
    request.session['info'] = resp
    openid = resp['openid']

    try:
        user = User.objects.get(username=openid)
    except User.DoesNotExist,e:
        print "user not exist"
        user = User.objects.create_user(openid,password=openid)
Пример #2
0
def authorization():
    code = request.args.get('code')
    api = WeixinAPI(appid=APP_ID,
                    app_secret=APP_SECRET,
                    redirect_uri=REDIRECT_URI)
    auth_info = api.exchange_code_for_access_token(code=code)
    api = WeixinAPI(access_token=auth_info['access_token'])
    resp = api.user(openid=auth_info['openid'])
    return jsonify(resp)
Пример #3
0
def authorization():
    code = request.args.get('code')
    api = WeixinAPI(appid=APP_ID,
                    app_secret=APP_SECRET,
                    redirect_uri=REDIRECT_URI)
    auth_info = api.exchange_code_for_access_token(code=code)
    api = WeixinAPI(access_token=auth_info['access_token'])
    resp = api.user(openid=auth_info['openid'])
    return jsonify(resp)
Пример #4
0
def weixin_authorization(request):
    code = request.GET.get('code')
    api = WeixinAPI(
        appid=settings.APP_ID,
        app_secret=settings.APP_SECRERT,
        redirect_uri=settings.REDIRECT_URI
    )

    auth_info = api.exchange_code_for_access_token(code=code)
    api = WeixinAPI(access_token=auth_info['access_token'])
    resp = api.user(openid=auth_info['openid'])
    return Response(data=resp, status=status.HTTP_200_OK)
Пример #5
0
    def get(self, request):
        print(1111111111)
        code = request.GET.get('code')

        print(code)
        api = WeixinAPI(appid="wxbb19e286afae10db",
                        app_secret="064004a0dd2a8a4c0eeed40975e8907c",
                        redirect_uri="http://127.0.0.1:8000/authorization")
        auth_info = api.exchange_code_for_access_token(code=code)
        api = WeixinAPI(access_token=auth_info['access_token'])
        resp = api.user(openid=auth_info['openid'])
        return redirect(
            "http://127.0.0.1:8000/taskpub?nickname={}&headimgurl={}".format(
                resp["nickname"], resp["headimgurl"]))
Пример #6
0
def adminAuthorization(request):
    """
    获取用户信息,登录,跳转
    :param request:
    :return:
    """
    auth_redirect_uri = DOMAIN + "adminAuthorization"
    code = request.GET.get('code')
    api = WeixinAPI(appid=APP_ID,
	    app_secret=APP_SECRET,
	    redirect_uri=auth_redirect_uri)
    auth_info = api.exchange_code_for_access_token(code=code)
    api = WeixinAPI(access_token=auth_info['access_token'])
    resp = api.user(openid=auth_info['openid'])
    request.session['info'] = resp
    return redirect(DOMAIN + 'administor/view/index.html')
Пример #7
0
def authorization():
    '''用户验证'''
    code = request.args.get('code')
    api = WeixinAPI(appid=APP_ID,
                    app_secret=APP_SECRET,
                    redirect_uri=REDIRECT_URI)
    auth_info = api.exchange_code_for_access_token(code=code)
    api = WeixinAPI(access_token=auth_info['access_token'])
    resp = api.user(openid=auth_info['openid'])

    with app.test_request_context():
        if check_user(auth_info['openid']):
            requests.post(data=resp)
        else:
            requests.put(data=resp)

    return jsonify(code=0)
Пример #8
0
api = WeixinAPI(appid=APP_ID, app_secret=APP_SECRET, redirect_uri=REDIRECT_URI)

try:
    pass
    # print api.get_authorize_login_url(scope=("snsapi_login",))
    # print api.exchange_code_for_access_token(code=code)
except OAuth2AuthExchangeError as e:
    print(e)

auth_info = {
    'access_token':
    'OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw-NdEN9dPkEX8Yewsve2AktmzS0gmbvzRKO49l6sxHRfhXg1no5ObdGufYhRIubP2m3FUdv-Cop3t3S_xwMbBWQ',
    'refresh_token':
    'OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw44bjXRXdmPsclqGIjWs777H3p00QI9a3hzX265Uq9fPJZttNQApdCRPbySXDfofbjniiwsVJiT7fTv7j5jCAxg',
    'openid': u'oV02tuA8Wt6Kk7S0pVydThYvmSJA',
    'expires_in': 7200,
    'scope': u'snsapi_login'
}

try:
    api.exchange_refresh_token_for_access_token(
        refresh_token=auth_info['refresh_token'])
except OAuth2AuthExchangeError:
    pass
api = WeixinAPI(access_token=auth_info['access_token'])
r = api.user(openid=auth_info['openid'])
print(r)
v = api.validate_token(openid=auth_info['openid'])
print(v)
 def getUserInfo(code):
     auth_info = WechatUser.api.exchange_code_for_access_token(code=code)
     data = WeixinAPI(access_token=auth_info['access_token'])
     return data.user(openid=auth_info['openid'])
Пример #10
0
REDIRECT_URI = 'http://www.exmple.com'


code = '0418f2c46cd26e4f9eee5bf03320662M'

api = WeixinAPI(appid=APP_ID,
                app_secret=APP_SECRET,
                redirect_uri=REDIRECT_URI)

try:
    pass
    # print api.get_authorize_login_url(scope=("snsapi_login",))
    # print api.exchange_code_for_access_token(code=code)
except OAuth2AuthExchangeError, e:
    print e


auth_info = {
    'access_token': 'OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw-NdEN9dPkEX8Yewsve2AktmzS0gmbvzRKO49l6sxHRfhXg1no5ObdGufYhRIubP2m3FUdv-Cop3t3S_xwMbBWQ',
    'refresh_token': 'OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw44bjXRXdmPsclqGIjWs777H3p00QI9a3hzX265Uq9fPJZttNQApdCRPbySXDfofbjniiwsVJiT7fTv7j5jCAxg',
    'openid': u'oV02tuA8Wt6Kk7S0pVydThYvmSJA',
    'expires_in': 7200,
    'scope': u'snsapi_login'}

print api.exchange_refresh_token_for_access_token(refresh_token=auth_info['refresh_token'])
api = WeixinAPI(access_token=auth_info['access_token'])
r = api.user(openid=auth_info['openid'])
print r
v = api.validate_token(openid=auth_info['openid'])
print v
Пример #11
0
api = WeixinAPI(appid=APP_ID, app_secret=APP_SECRET, redirect_uri=REDIRECT_URI)

try:
    pass
    # print api.get_authorize_login_url(scope=("snsapi_login",))
    # print api.exchange_code_for_access_token(code=code)
except OAuth2AuthExchangeError as e:
    print(e)

auth_info = {
    "access_token":
    "OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw-NdEN9dPkEX8Yewsve2AktmzS0gmbvzRKO49l6sxHRfhXg1no5ObdGufYhRIubP2m3FUdv-Cop3t3S_xwMbBWQ",
    "refresh_token":
    "OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw44bjXRXdmPsclqGIjWs777H3p00QI9a3hzX265Uq9fPJZttNQApdCRPbySXDfofbjniiwsVJiT7fTv7j5jCAxg",
    "openid": u"oV02tuA8Wt6Kk7S0pVydThYvmSJA",
    "expires_in": 7200,
    "scope": u"snsapi_login",
}

try:
    api.exchange_refresh_token_for_access_token(
        refresh_token=auth_info["refresh_token"])
except OAuth2AuthExchangeError:
    pass
api = WeixinAPI(access_token=auth_info["access_token"])
r = api.user(openid=auth_info["openid"])
print(r)
v = api.validate_token(openid=auth_info["openid"])
print(v)
Пример #12
0
APP_SECRET = "your app secret"
REDIRECT_URI = "https://passport.yhd.com/wechat/callback.do"


code = "021b66a31b7179822b01e7e2c12528cV"

api = WeixinAPI(appid=APP_ID, app_secret=APP_SECRET, redirect_uri=REDIRECT_URI)

try:
    print api.get_authorize_login_url(scope=("snsapi_login",))
    print api.exchange_code_for_access_token(code=code)
except OAuth2AuthExchangeError, e:
    print e

auth_info = {
    "access_token": "OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw-NdEN9dPkEX8Yewsve2AktmzS0gmbvzRKO49l6sxHRfhXg1no5ObdGufYhRIubP2m3FUdv-Cop3t3S_xwMbBWQ",
    "refresh_token": "OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw44bjXRXdmPsclqGIjWs777H3p00QI9a3hzX265Uq9fPJZttNQApdCRPbySXDfofbjniiwsVJiT7fTv7j5jCAxg",
    "openid": u"oV02tuA8Wt6Kk7S0pVydThYvmSJA",
    "expires_in": 7200,
    "scope": u"snsapi_login",
}

print api.exchange_refresh_token_for_access_token(refresh_token=auth_info["refresh_token"])

api = WeixinAPI(access_token=auth_info["access_token"])

r = api.user(openid=auth_info["openid"])
print r
v = api.validate_token(openid=auth_info["openid"])
print v