预授权码(pre_auth_code)是第三方平台方实现授权托管的必备信息,每个预授权码有效期为 10 分钟。需要先获取令牌才能调用
请求地址:
POST https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=COMPONENT_ACCESS_TOKEN
需要自行保存preAuthCode
上代码:
/** * @Description 获取预授权码 * @param * @author zxc */ @Transactional(rollbackFor = Exception.class) @Override public String preAuthCode() { log.info(START_GET_PRE_AUTH_CODE); String preAuthCode = ""; String accessToken = redisThird.getComponentAccessToken(); JSONObject jsonObject = new JSONObject(); jsonObject.put(ModuleConstant.COMPONENT_APPID, componentAppId); String post = HttpClientManager.getInstance().sendPostByJSON("https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=COMPONENT_ACCESS_TOKEN"+ accessToken, JSON.toJSONString(jsonObject)).getData(); log.info(String.format(POST_RESULT,post)); Map hashMap = JSON.parseObject(post, Map.class); if (!hashMap.containsKey("errmsg")) { preAuthCode = hashMap.get("pre_auth_code").toString(); Integer expiresIn = (Integer) hashMap.get(“expires_in”); Date expiresInTime = this.countExpirationTime(expiresIn.toString()); if (StringUtils.isNotEmpty(preAuthCode)) { //预授权码 存DB PreAuthTokenFormDTO formDTO = new PreAuthTokenFormDTO(); formDTO.setPreAuthToken(preAuthCode); formDTO.setExpiresInTime(expiresInTime); //先逻辑删除,在插入DB preAuthTokenDao.deleteOldPreAuthCode(); preAuthTokenDao.insertPreAuthToken(formDTO); //预授权码 放入缓存 redisThird.setPreAuthCode(preAuthCode); } else { throw new RuntimeException(FAILURE_GET_PRE_AUTH_CODE); } log.info(END_GET_PRE_AUTH_CODE); } return preAuthCode; }
无需返回,void即可,我这为了方便使用,把preAuthCode返回了......