三、预授权码(pre_auth_code)
3.1 官网说明:
第三方平台方实现授权托管的必备信息,每个预授权码有效期为 10 分钟。需要先获取令牌才能调用,生成授权链接时使用。
请求参数说明:
【注意】:官方网站上,参数component_access_token写在了URL中,参数component_appid写在请求体中。
POST数据示例:
{
"component_appid": "appid_value"
}
1.2 获取流程及示意代码
通获取令牌的流程类似。
/**
* 获取预授权码,判断缓存中是否存在,若不存在,则走直接获取流程
* @return
*/
public static String getPreAuthCode(){
webChatCatchUtil = SpringContextHolder.getBean("webChatCatchUtil");
String authCode = webChatCatchUtil.getWeixinOpenPreAuthCode();
if(authCode==null) {
authCode = OpAuthCodeUtil.getPreAuthCodeInstant();
}
return authCode;
}
/**
* 直接获取流程
* @return
*/
private static String getPreAuthCodeInstant(){
webChatCatchUtil = SpringContextHolder.getBean("webChatCatchUtil");
String accessToken = getOpenAccToken();
//设置请求参数
JSONObject json = new JSONObject();
json.put("component_appid", ComponentConfig.APPID);
String API_COMPONENT_TOKEN_URl = WeixinImSetting.OPEN_URL_PREAUTHCODE;
String responseData = HttpSend.httpPost(API_COMPONENT_TOKEN_URl + accessToken, json.toString(), null); //发送HTTPS请求
JSONObject postData = JSONObject.fromObject(responseData);
log.info("====================返回post结果:" + postData);
String authCode = (String) postData.get("pre_auth_code");
Integer expires_in = (Integer) postData.get("expires_in"); //有效期,单位:秒
log.info("getPreAuthCodeInstant-result:"+json);
webChatCatchUtil.setWeixinOpenPreAuthCode(authCode,expires_in); //存入缓存
return authCode;
}
返回结果说明:
返回结果示例:pre_auth_code(预授权码),expires_in(有效期,秒)
{
"pre_auth_code": "Cx_Dk6qiBE0Dmx4EmlT3oRfArPvwSQ-oa3NL_fwHM7VI08r52wazoZX2Rhpz1dEw",
"expires_in": 600
}