微信第三方平台【五】获取/刷新接口调用令牌
程序员文章站
2024-02-08 10:56:16
...
在公众号/小程序接口调用令牌(authorizer_access_token)失效时,可以使用刷新令牌(authorizer_refresh_token)获取新的接口调用令牌。
注意: authorizer_access_token 有效期为 2 小时,开发者需要缓存 authorizer_access_token,避免获取/刷新接口调用令牌的 API 调用触发每日限额
请求地址:
POST https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=COMPONENT_ACCESS_TOKEN
上代码:
public void refreshToken() { //查询 即将过期的 authorizer_access_token List<WillOverDueResultDTO> willOverDueResultDTOS = authorizationInfoDao.checkWillOverDue(); if (willOverDueResultDTOS.size() != NumConstant.ZERO && null != willOverDueResultDTOS){ log.info("查询到即将过期的authorizer_access_token"); willOverDueResultDTOS.forEach(willOverDueDTO -> { String authAppId = willOverDueDTO.getAuthAppId(); String customerId = willOverDueDTO.getCustomerId(); String clientType = willOverDueDTO.getClientType(); JSONObject jsonObject = new JSONObject(); //授权方AppId jsonObject.put(ModuleConstant.AUTHORIZER_APPID,authAppId); //第三方平台AppId jsonObject.put(ModuleConstant.COMPONENT_APPID,componentAppId); jsonObject.put(ModuleConstant.AUTHORIZER_REFRESH_TOKEN,willOverDueDTO.getAuthorizerRefreshToken()); String componentAccessToken = redisThird.getComponentAccessToken(); String data = HttpClientManager.getInstance().sendPostByJSON("https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=COMPONENT_ACCESS_TOKEN" + componentAccessToken, JSON.toJSONString(jsonObject)).getData(); Map map = JSON.parseObject(data, HashMap.class); if (!map.containsKey(ModuleConstant.ERR_CODE)) { log.info(REFRESH_AUTH_ACCESS_TOKEN); String authorizerAccessToken = map.get(ModuleConstant.AUTHORIZER_ACCESS_TOKEN).toString(); String expiresIn = map.get(ModuleConstant.EXPIRES_IN).toString(); String authorizerRefreshToken = map.get(ModuleConstant.AUTHORIZER_REFRESH_TOKEN).toString(); Date expiresInTime = this.countExpirationTime(expiresIn); //更新DB AuthorizerAccessTokenFormDTO formDTO = new AuthorizerAccessTokenFormDTO(); formDTO.setAuthorizerAccessToken(authorizerAccessToken); formDTO.setAuthorizerRefreshToken(authorizerRefreshToken); formDTO.setExpiresInTime(expiresInTime); formDTO.setCustomerId(customerId); formDTO.setAuthAppid(authAppId); formDTO.setClientType(clientType); AuthorizationInfoFormDTO authorizationInfo = new AuthorizationInfoFormDTO(); BeanUtils.copyProperties(formDTO,authorizationInfo); authorizationInfo.setAuthorizerAppid(authAppId); //先逻辑删除,在插入 authorizationInfoDao.deleteOldAuthorizerAccessToken(customerId, clientType); authorizationInfoDao.insertAuthorizerAccessToken(formDTO); //缓存 refreshAuthorizerAccessToken redisThird.setAuthorizerRefreshToken(authorizationInfo); AuthorizationInfoResultDTO resultDTO = new AuthorizationInfoResultDTO(); resultDTO.setAuthorizer_access_token(authorizerAccessToken); resultDTO.setAuthorizer_refresh_token(authorizerRefreshToken); resultDTO.setAuthorizer_appid(authAppId); resultDTO.setExpires_in(7200); redisThird.setAuthInfo(resultDTO,customerId,clientType); } }); } }
上一篇: Ajax与Axios的区别
下一篇: HTTP请求工具类,支持https