微信小程序> 微信第三方平台开发经验总结(三):获取pre_auth_code

微信第三方平台开发经验总结(三):获取pre_auth_code

浏览量:696 时间: 来源:江月寒空
获取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;}

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎