折腾:
之后,又出现另外的问题:
页面一直跳转,无法结束了。
最后终止了服务器,才看到502的错误,页面才停止:
log是:
(SIPEvents) ➜ SIPEvents gunicorn -w 4 -b 127.0.0.1:8080 wechat_auth:app [2016-08-20 17:25:25 +0000] [13934] [INFO] Starting gunicorn 19.6.0 [2016-08-20 17:25:25 +0000] [13934] [INFO] Listening at: http://127.0.0.1:8080 (13934) [2016-08-20 17:25:25 +0000] [13934] [INFO] Using worker: sync [2016-08-20 17:25:25 +0000] [13939] [INFO] Booting worker with pid: 13939 [2016-08-20 17:25:25 +0000] [13940] [INFO] Booting worker with pid: 13940 [2016-08-20 17:25:25 +0000] [13941] [INFO] Booting worker with pid: 13941 [2016-08-20 17:25:26 +0000] [13949] [INFO] Booting worker with pid: 13949 <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:110]: signature=4596a4d3eb2bd886f5a7cdf37c0ea0f717e83877, timestamp=1471685130, nonce=650215230, echostr= <div–<—————————————————————————— tmp_list= [‘sipevents’, u’1471685130′, u’650215230′] after sort: tmp_list= [u’1471685130′, u’650215230′, ‘sipevents’] tmp_str= 1471685130650215230sipevents tmp_str_encoded= 1471685130650215230sipevents tmp_str_sha1= <sha1 HASH object @ 0x7f97e3f0d300> tmp_str_hexdigest= 4596a4d3eb2bd886f5a7cdf37c0ea0f717e83877 signature= 4596a4d3eb2bd886f5a7cdf37c0ea0f717e83877 signature OK <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:114]: wechat check_signature OK <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:122]: requestMethod=POST <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:125]: requestData=<xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1471685130</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:132]: requestArgs=ImmutableMultiDict([(‘nonce’, u’650215230′), (‘timestamp’, u’1471685130′), (‘signature’, u’4596a4d3eb2bd886f5a7cdf37c0ea0f717e83877′), (‘openid’, u’oswjmv4X0cCXcfkIwjoDfCkeTVVY’)]) <div–<—————————————————————————— data= <xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1471685130</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:144]: parse post body data OK <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:157]: messageId=0, messageTarget=gh_ac090a9873a8, messageSource=oswjmv4X0cCXcfkIwjoDfCkeTVVY, messageTime=1471685130, messageType=view, messageRaw=<xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1471685130</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:167]: respStr=Not Support type of POST <div–<—————————————————————————— text response= <xml> <ToUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></ToUserName> <FromUserName><![CDATA[gh_ac090a9873a8]]></FromUserName> <CreateTime>1471685130</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: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 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:47]: requestArgs=ImmutableMultiDict([(‘state’, u”), (‘code’, u’031WO5aY1rhSk61e47aY1tT0aY1WO5a-‘)]) <div–<—————————————————————————— <div–<—————————————————————————— 。。。。。。 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:47]: requestArgs=ImmutableMultiDict([(‘state’, u”), (‘code’, u’021WuaS22U8RB111JlO22zD9S22WuaS0′)]) <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 <div–<—————————————————————————— ^C[2016-08-20 17:25:39 +0000] [13934] [INFO] Handling signal: int [2016-08-20 17:25:39 +0000] [13939] [INFO] Worker exiting (pid: 13939) [2016-08-20 17:25:39 +0000] [13941] [INFO] Worker exiting (pid: 13941) [2016-08-20 17:25:39 +0000] [13949] [INFO] Worker exiting (pid: 13949) [2016-08-20 17:25:39 +0000] [13940] [INFO] Worker exiting (pid: 13940) [2016-08-20 17:25:39 +0000] [13934] [INFO] Shutting down: Master |
看来是需要写代码,确保正确处理的:
当可以获得code参数,就停止跳转,就继续执行其它的逻辑的。
代码改为:
@app.route("/") def hello(): requestArgs = request.args app.logger.debug(‘requestArgs=%s’, requestArgs) needRedirect = False paraCode = request.args.get(‘code’, ”) app.logger.debug(‘paraCode=%s’, paraCode) if not paraCode : needRedirect = True paraState = request.args.get(‘state’, ”) app.logger.debug(‘paraState=%s’, paraState) # if not paraState : # needRedirect = True if needRedirect: redirect_uri = "http://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) else: callbackOkStr = u"回调成功: code=%s, state=%s" % (paraCode, paraState) return make_response(callbackOkStr) |
界面效果:
服务器log:
(SIPEvents) ➜ SIPEvents gunicorn -w 4 -b 127.0.0.1:8080 wechat_auth:app [2016-08-20 17:44:06 +0000] [14210] [INFO] Starting gunicorn 19.6.0 [2016-08-20 17:44:06 +0000] [14210] [INFO] Listening at: http://127.0.0.1:8080 (14210) [2016-08-20 17:44:06 +0000] [14210] [INFO] Using worker: sync [2016-08-20 17:44:06 +0000] [14215] [INFO] Booting worker with pid: 14215 [2016-08-20 17:44:06 +0000] [14218] [INFO] Booting worker with pid: 14218 [2016-08-20 17:44:06 +0000] [14221] [INFO] Booting worker with pid: 14221 [2016-08-20 17:44:06 +0000] [14224] [INFO] Booting worker with pid: 14224 <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:126]: signature=e42ae34dd493ce2a3f7da60265bfc2760b37d63e, timestamp=1471686247, nonce=1745974408, echostr= <div–<—————————————————————————— tmp_list= [‘sipevents’, u’1471686247′, u’1745974408′] after sort: tmp_list= [u’1471686247′, u’1745974408′, ‘sipevents’] tmp_str= 14716862471745974408sipevents tmp_str_encoded= 14716862471745974408sipevents tmp_str_sha1= <sha1 HASH object @ 0x7f606ce172b0> tmp_str_hexdigest= e42ae34dd493ce2a3f7da60265bfc2760b37d63e signature= e42ae34dd493ce2a3f7da60265bfc2760b37d63e signature OK <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:130]: wechat check_signature OK <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:138]: requestMethod=POST <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:141]: requestData=<xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1471686247</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:148]: requestArgs=ImmutableMultiDict([(‘nonce’, u’1745974408′), (‘timestamp’, u’1471686247′), (‘signature’, u’e42ae34dd493ce2a3f7da60265bfc2760b37d63e’), (‘openid’, u’oswjmv4X0cCXcfkIwjoDfCkeTVVY’)]) <div–<—————————————————————————— data= <xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1471686247</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:160]: parse post body data OK <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:173]: messageId=0, messageTarget=gh_ac090a9873a8, messageSource=oswjmv4X0cCXcfkIwjoDfCkeTVVY, messageTime=1471686247, messageType=view, messageRaw=<xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1471686247</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:183]: respStr=Not Support type of POST <div–<—————————————————————————— text response= <xml> <ToUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></ToUserName> <FromUserName><![CDATA[gh_ac090a9873a8]]></FromUserName> <CreateTime>1471686247</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:95]: requestArgs=ImmutableMultiDict([]) <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:100]: paraCode= <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:105]: paraState= <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:112]: 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 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:95]: requestArgs=ImmutableMultiDict([(‘state’, u”), (‘code’, u’041RHfws1XAWfr0wF1us1y7nws1RHfwp’)]) <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:100]: paraCode=041RHfws1XAWfr0wF1us1y7nws1RHfwp <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in wechat_auth [/root/html/SIPEvents/wechat_auth.py:105]: paraState= <div–<—————————————————————————— |
[总结]
此处的正确逻辑就是去判断,当有回调的url中已经获得了code参数,就不继续跳转了。即可。
转载请注明:在路上 » [已解决]微信授权出问题:页面无限循环跳转始终无法结束