折腾:
[记录]用Python版微信SDK wechat-sdk去给微信公众号授权
期间,
然后对于代码:
@app.route("/") def hello(): requestArgs = request.args app.logger.debug(‘requestArgs=%s’, requestArgs) # return "欢迎来到金鸡日历!" redirect_uri = "hd.webonn.com" authorize_url = wechat.generate_oauth2_authorize_url(redirect_uri) app.logger.debug(‘redirect_uri=%s, authorize_url=%s’, redirect_uri, authorize_url) return redirect(authorize_url) |
库是:
def generate_oauth2_authorize_url(self, redirect_uri, response_type="code", scope="snsapi_userinfo", state=""): """ 生成获取用户信息的url :param redirect_uri: 授权后重定向的回调链接地址,该方法内自动使用urlencode对链接进行处理 :param response_type: 返回类型,默认为code :param scope: 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid), snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。 并且,即使在未关注的情况下,只要用户授权,也能获取其信息)。 默认为snsapi_userinfo :param state: 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节 :return: 用户授权的url链接字符串 """ redirect_uri = urllib.quote(redirect_uri) url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=%s&scope=%s&state=%s#wechat_redirect" % ( self.__appid, redirect_uri, response_type, scope, state ) return url |
运行结果:
redirect_uri参数错误
log:
(SIPEvents) ➜ SIPEvents gunicorn -w 4 -b 127.0.0.1:8080 wechat_auth:app [2016-08-20 17:17:41 +0000] [13840] [INFO] Starting gunicorn 19.6.0 [2016-08-20 17:17:41 +0000] [13840] [INFO] Listening at: http://127.0.0.1:8080 (13840) [2016-08-20 17:17:41 +0000] [13840] [INFO] Using worker: sync [2016-08-20 17:17:41 +0000] [13845] [INFO] Booting worker with pid: 13845 [2016-08-20 17:17:41 +0000] [13846] [INFO] Booting worker with pid: 13846 [2016-08-20 17:17:41 +0000] [13847] [INFO] Booting worker with pid: 13847 [2016-08-20 17:17:41 +0000] [13852] [INFO] Booting worker with pid: 13852 <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:111]: signature=187535e50afe6d1abceb5e141fa13105c1541d31, timestamp=1471684664, nonce=1121707528, echostr= <div–<—————————————————————————— tmp_list= [‘sipevents’, u’1471684664′, u’1121707528′] after sort: tmp_list= [u’1121707528′, u’1471684664′, ‘sipevents’] tmp_str= 11217075281471684664sipevents tmp_str_encoded= 11217075281471684664sipevents tmp_str_sha1= <sha1 HASH object @ 0x7efdc7fce300> tmp_str_hexdigest= 187535e50afe6d1abceb5e141fa13105c1541d31 signature= 187535e50afe6d1abceb5e141fa13105c1541d31 signature OK <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:115]: wechat check_signature OK <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:123]: requestMethod=POST <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:126]: requestData=<xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1471684664</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[VIEW]]></Event> <EventKey><![CDATA[http://hd.webonn.com/]]></EventKey> <MenuId>408577226</MenuId> </xml> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:133]: requestArgs=ImmutableMultiDict([(‘nonce’, u’1121707528′), (‘timestamp’, u’1471684664′), (‘signature’, u’187535e50afe6d1abceb5e141fa13105c1541d31′), (‘openid’, u’oswjmv4X0cCXcfkIwjoDfCkeTVVY’)]) <div–<—————————————————————————— data= <xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1471684664</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[VIEW]]></Event> <EventKey><![CDATA[http://hd.webonn.com/]]></EventKey> <MenuId>408577226</MenuId> </xml> self.conf.encrypt_mode= normal <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:145]: parse post body data OK <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:158]: messageId=0, messageTarget=gh_ac090a9873a8, messageSource=oswjmv4X0cCXcfkIwjoDfCkeTVVY, messageTime=1471684664, messageType=view, messageRaw=<xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1471684664</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[VIEW]]></Event> <EventKey><![CDATA[http://hd.webonn.com/]]></EventKey> <MenuId>408577226</MenuId> </xml> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:168]: respStr=Not Support type of POST <div–<—————————————————————————— text response= <xml> <ToUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></ToUserName> <FromUserName><![CDATA[gh_ac090a9873a8]]></FromUserName> <CreateTime>1471684664</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[Not Support type of POST]]></Content> </xml> <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:48]: requestArgs=ImmutableMultiDict([]) <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:52]: redirect_uri=hd.webonn.com, authorize_url=https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx906274ff0f9165dd&redirect_uri=hd.webonn.com&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:48]: requestArgs=ImmutableMultiDict([]) <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:52]: redirect_uri=hd.webonn.com, authorize_url=https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx906274ff0f9165dd&redirect_uri=hd.webonn.com&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect <div–<—————————————————————————— |
对于:
redirect_uri=hd.webonn.com, authorize_url=https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx906274ff0f9165dd&redirect_uri=hd.webonn.com&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect |
参考:
的:
Scope为snsapi_userinfo https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect |
redirect_uri是加了http的
所以去改为:
redirect_uri = "http://hd.webonn.com" |
结果:
就可以避免该问题了:
可以正常输出log了:
DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:47]: requestArgs=ImmutableMultiDict([]) <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:51]: redirect_uri=http://hd.webonn.com, authorize_url=https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx906274ff0f9165dd&redirect_uri=http%3A//hd.webonn.com&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect |
然后跳转回来,收到了code:
requestArgs=ImmutableMultiDict([(‘state’, u”), (‘code’, u’021WuaS22U8RB111JlO22zD9S22WuaS0′)]) |
[总结]
在折腾微信授权的时候,此处在代码中,去写:
redirect_uri
的话,则是需要写完整的(带http前缀的)地址的:
比如:
而不是:
之前在微信后台配置有效的回调url的时候的,是不带http前缀的:
hd.webonn.com
这样,就可以正常跳转,就不会出现:
redirect_uri参数错误
的错误了,即可以正常跳转。
跳转后,对应的url,就可以接收到request的参数,其中带了code参数了。
但是又出现别的问题:
转载请注明:在路上 » [已解决]微信授权失败:redirect_uri参数错误