获取(刷新)授权公众号或小程序的接口调用凭据(令牌)
该API用于在授权方令牌(authorizer_access_token)失效时,可用刷新令牌(authorizer_refresh_token)获取新的令牌。请注意,此处token是2小时刷新一次,开发者需要自行进行token的缓存,避免token的获取次数达到每日的限定额度。缓存方法可以参考:http://mp.weixin.qq.com/wiki/2/88b2bf1265a707c031e51f26ca5e6512.html
当换取authorizer_refresh_token后建议保存。
public void updateToken() { List<AuthorizerInfo> list = authorizerDao.findAllAuthorizer(); for (AuthorizerInfo authorizerInfo :list){ AuthorizerInfo authorizer = authorizerDao.findAuthorizerByAppId(authorizerInfo.getAuthorizer_appid()); ConfigInfo config = configDao.findConfigById(); String token = config.getComponent_access_token(); String data; try { Map<String, Object> map = new HashMap<>(); if (authorizer!=null) { map.put("component_appid", COMPONENT_APPID); map.put("authorizer_appid", authorizer.getAuthorizer_appid()); map.put("authorizer_refresh_token", authorizer.getAuthorizer_refresh_token()); data = HttpRequestor.doPost( "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" + token, GsonUtil.objectToJson(map)); Map<String, Object> resultData = GsonUtil.parseJsonWithGson(data, Map.class); String str = "\""; String authorizer_access_token = GsonUtil.objectToJson(resultData.get("authorizer_access_token")) .replace(str, ""); String authorizer_refresh_token = GsonUtil.objectToJson(resultData.get("authorizer_refresh_token")) .replace(str, ""); if(authorizer_access_token!=null&&authorizer_refresh_token!=null){ authorizer.setAuthorizer_access_token(authorizer_access_token); authorizer.setAuthorizer_refresh_token(authorizer_refresh_token); authorizerDao.updateAuthorizerByAppId(authorizer); }else { logger.error(authorizer.getAuthorizer_appid()+"为null....."); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }