获取pre_auth_code

该API用于获取预授权码。预授权码用于公众号或小程序授权时的第三方平台方安全验证。

接口调用请求说明

http请求方式: POST(请使用https协议)

https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=xxx

POST数据示例:

{"component_appid":"appid_value" }这个api和上一个差不多
  • 判断pre_auth_code未过期直接返回
  • 获取token,如果过期则重新请求
  • 替换url中的xxx
  • 拼装json
  • 发送请求并获取返回结果
  • 保存pre_auth_code
@Overridepublic String getPreAuthCode() {    String preAuthCode = ComponentInfo.getPreAuthCode();    /**判断数据库中是否存在pre_auth_code*/    if(!preAuthCode.isEmpty()){        /**如果存在,直接返回pre_auth_code的值*/        return preAuthCode;    }    /**token若不存在,重新请求token*/    String componentAccessToken = ComponentInfo.getComponentAccessToken().isEmpty()?getComponentAccessToken():ComponentInfo.getComponentAccessToken();    logger.info("ThirdPartyServiceImpl:getPreAuthCode:componentAccessToken={}",componentAccessToken);    /**替换Url中的{component_access_token}*/    String url = ThirdPartyConfig.CREATE_PREAUTHCODE_URL.replace("{component_access_token}",componentAccessToken);    logger.info("ThirdPartyServiceImpl:getPreAuthCode:url={}",url);    JSONObject json = new JSONObject();    json.accumulate("component_appid",ThirdPartyConfig.APP_ID);    /**发送Https请求到微信*/    String retStr = HttpsUtil.postHtpps(url,json.toString());    logger.info("ThirdPartyServiceImpl:getPreAuthCode:retStr={}",retStr);    JSONObject resultJson = JSONObject.fromObject(retStr);    /**在返回结果中获取pre_auth_code*/    preAuthCode = resultJson.getString("pre_auth_code");    /**保存pre_auth_code,并设置有效时间*/    ComponentInfo.setPreAuthCode(preAuthCode);    return preAuthCode;}

TAG标签: 微信小程序预授权

http://www.xiaochengxub.com/read-104495.html