项目存放accessToken 皆是一redis存储
1. 获得Access Token
并且获取jsapi_ticket
accessToken 存在有效期 所以,用定时任务 定时刷新
WeChatServiceImpl
一下几个方法来处理:
/** * 刷新AccessToken * 仅在 接口返回token失效时 * @return */ @Override public boolean flushWeChatAccessToken() throws Exception{ // 获取AccessToken String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; String appid = PropertiesReader.readString("weChat","server_appId"); String secret = PropertiesReader.readString("weChat","server_appSecret"); requestUrl = String.format(requestUrl, appid, secret); JSONObject accessTokenObject = HttpClientUtil.getToHttps(requestUrl); String accessToken = accessTokenObject.getString("access_token"); int expireTime = accessTokenObject.getInteger("expires_in"); setAccessToken(Const.WEIXINACCESSTOKEN,accessToken,expireTime); // 获取jsticket String jsticketRequestUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi"; jsticketRequestUrl = String.format(jsticketRequestUrl, accessToken); JSONObject jsticketObject = HttpClientUtil.getToHttps(jsticketRequestUrl); String ticket = jsticketObject.getString("ticket");