wechat-token-server 微信token中控服务器,用于统一获取并缓存微信开发中使用的access_token和jsticket。 wechat-token-server是一个自动定时刷新微信token的服务,可以每隔一段时间自动获取token,保存在redis中,通过访问redis或web接口即可获取到缓存的token值。同时用户也可以主动刷新。 github源码地址:https://github.com/shangyexin/wechat-token-server 
主要实现代码 使用tornado异步http client定时刷新token 异步请求:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 async  def  refreshToken (tokenType ):try :await  asyncHttpClient.fetch(request)except  Exception as  e:'Exception: %s' , e)'Create a request for %s failed, will retry after 10s'  % str (tokenType))10 , refreshToken, tokenType)else :'Async httpclient response: %s' , response)'Async httpclient response body: %s' , response.body)'utf8' ))'errcode'  in  resDictif  errcodeExist == False  or  resDict['errcode' ] == 0 :'Request %s success, response is :%s'  % (tokenType, resDict))try :set (tokenType, token, ex=config.tokenExpireTime)'Set %s in redis success.' , tokenType)except  Exception as  e:else :'Request for %s failed, error message is:'  % str (tokenType))'errmsg' ])10 , refreshToken, tokenType)
定时刷新:
1 2 3 4 5 6 7 8 async  def  refreshAllTokens ():'Begin to refresh all tokens...' )while  True :for  tokenType in  config.tokenSources.keys():await  refreshToken(tokenType)await  tornado.gen.sleep(config.tokenExpireTime)
开始:
1 2 
使用tornado同步http client强制刷新 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class  ForceRefreshHandler (tornado.web.RequestHandler):def  get (self ):self .get_argument('type' , 'UnKnown' )self .get_argument('secret' , 'UnKnown' )if  tokenType != 'UnKnown'  and  secret == config.requestSecret:try :except  Exception as  e:'Exception: %s' , e)'Force refresh %s failed.'  % str (tokenType))'errmsg' ] = str (e)else :'Sync httpclient response: %s' , response)'Sync httpclient response body: %s' , response.body)'utf8' ))'Force refresh %s success, response is :%s'  % (tokenType, resDict))try :set (tokenType, token, ex=config.tokenExpireTime)'Set %s in redis success.' , tokenType)except  Exception as  e:'expires_in' ] = config.tokenExpireTimeself .write(json.dumps(ret, sort_keys=False ))
实现功能(微信官方建议) 
建议公众号开发者使用中控服务器统一获取和刷新Access_token,其他业务逻辑服务器所使用的access_token均来自于该中控服务器,不应该各自去刷新,否则容易造成冲突,导致access_token覆盖而影响业务;
目前Access_token的有效期通过返回的expire_in来传达,目前是7200秒之内的值。中控服务器需要根据这个有效时间提前去刷新新access_token。在刷新过程中,中控服务器可对外继续输出的老access_token,此时公众平台后台会保证在5分钟内,新老access_token都可用,这保证了第三方业务的平滑过渡;
Access_token的有效时间可能会在未来有调整,所以中控服务器不仅需要内部定时主动刷新,还需要提供被动刷新access_token的接口,这样便于业务服务器在API调用获知access_token已超时的情况下,可以触发access_token的刷新流程。
 
配置 修改config.py即可,下面是各配置选项含义。 tokenExpireTime: 中控服务器自己缓存的token过期时间bindIp:tornado服务器绑定的IPbindPort: 绑定端口redisIp:redis服务器运行IPredisPort: redis服务器运行端口requestSecret: 获取token的口令appid:公众号或小程序应用IDsecret: 应用秘钥 示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 logging.config.fileConfig("logger.conf" )"wechat-access-token-server" )7000 '0.0.0.0' 12123 '127.0.0.1' 6379 '*******' 'access_token' ] = {'url' : 'https://api.weixin.qq.com/cgi-bin/token' ,'method' : 'GET' ,'args' : {'grant_type' : 'client_credential' ,'appid' : '**************' ,'secret' : '**********************' 'ticket' ] = {'url' : 'https://api.weixin.qq.com/cgi-bin/ticket/getticket' ,'method' : 'GET' ,'args' : {'type' : 'jsapi' ,'access_token' : '{{access_token}}' 
运行与使用 环境与依赖项 
Ubuntu16.04 
Python 3.6 
tornado==5.1.1 
redis==3.0.1 
 
后台运行 nohup python3 main.py &
通过redis查询 如果查询程序与wechat token sever运行在同一台机器上,可以直接查询redis服务器,键值就是access_token和ticket。
通过url访问 直接访问对应的url即可,secret为自己在config.py里面设置的值
查询access_token : http://127.0.0.1:12123/wechat/token?type=access_token&secret=f3b2241f967aa3c7966f537cdd82ce11 
查询jsticket : http://127.0.0.1:12123/wechat/token?type=ticket&secret=f3b2241f967aa3c7966f537cdd82ce11 
主动刷新access_token : http://127.0.0.1:12123/wechat/token/forcerefresh?type=access_token&secret=f3b2241f967aa3c7966f537cdd82ce11 
主动刷新jsticket : http://127.0.0.1:12123/wechat/token/forcerefresh?type=ticket&secret=f3b2241f967aa3c7966f537cdd82ce11 
 
结果示例 查询access_token结果: {"access_token": "16_XLwKyOP2XSxwr1ZCB0tpaANb4l2cJSeJdr6aj0QQkQAp_v4q5E-eCIkmDyOcMU6V0aTSsXiJzyf-KwiVP0MHIv47XftDa_oSvCnH8jJfTz8POfBjTcl52jGBboPMoo-esVjCVW2Ll3D2_6RHAGEiAJAGMK", "expires_in": 3298}
反向代理设置 如果希望通过域名访问,可以使用Nginx设置反向代理,将你需要访问域名配置好后,访问指定的url直接转发到tornado运行的地址与端口即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 server {
这样配置好后,在其他任何地方直接访问https://gitlab.net.cn/wechat/token?type=access_token&secret=f3b2241f967aa3c7966f537cdd82ce11即可得到access_token值。