六、接口调用令牌
6.1 官网说明
在公众号/小程序接口调用令牌(authorizer_access_token)失效时,可以使用刷新令牌(authorizer_refresh_token)获取新的接口调用令牌。
注意: authorizer_access_token 有效期为 2 小时,开发者需要缓存 authorizer_access_token,避免获取/刷新接口调用令牌的 API 调用触发每日限额。缓存方法可以参考:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
请求地址
POST https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=COMPONENT_ACCESS_TOKEN
请求参数说明
参数 |
类型 |
必填 |
说明 |
component_access_token |
string |
是 |
令牌 |
component_appid |
string |
是 |
第三方平台 appid |
authorizer_appid |
string |
是 |
授权方 appid |
authorizer_refresh_token |
string |
是 |
刷新令牌,获取授权信息时得到 |
POST 数据示例:
{
"component_appid": "appid_value",
"authorizer_appid": "auth_appid_value",
"authorizer_refresh_token": "refresh_token_value"
}
6.2 获取流程及示意代码
/**
* 获取接口调用令牌
* @param authorizerAppid 授权方 appid
* @param authorizerRefreshToken 授权方 刷新令牌
* @return
*/
public static String getAuthorizerAccessToken(String authorizerAppid,String authorizerRefreshToken){
webChatCatchUtil = SpringContextHolder.getBean("webChatCatchUtil");
String authorizerAccessToken = webChatCatchUtil.getWeixinOpenAuthorizerAccessToken();
if(authorizerAccessToken==null) {
authorizerAccessToken = OpAuthCodeUtil.getAuthorizerAccessTokenInstant(authorizerAppid,authorizerRefreshToken);
}
return authorizerAccessToken;
}
private static String getAuthorizerAccessTokenInstant(String authorizerAppid,String authorizerRefreshToken){
webChatCatchUtil = SpringContextHolder.getBean("webChatCatchUtil");
String componentAccessToken = getOpenAccToken(); //令牌
//设置请求参数
JSONObject json = new JSONObject();
json.put("component_appid", ComponentConfig.APPID);
json.put("authorizer_appid", authorizerAppid);
json.put("authorizer_refresh_token", authorizerRefreshToken);
String API_COMPONENT_TOKEN_URl = WeixinImSetting.OPEN_URL_AUTHORIZERTOKEN;
String responseData = HttpSend.httpPost(API_COMPONENT_TOKEN_URl + componentAccessToken, json.toString(), null);
JSONObject postData = JSONObject.fromObject(responseData);
log.info("====================返回post结果:" + postData);
String authorizerAccessToken = (String) postData.get("authorizer_access_token");
Integer expires_in = (Integer) postData.get("expires_in"); //有效期,单位:秒
log.info("getAuthorizerAccessTokenInstant-result:"+json);
webChatCatchUtil.setWeixinOpenPreAuthCode(authorizerAccessToken,expires_in);
return authorizerAccessToken;
}
结果参数说明
参数 |
类型 |
说明 |
authorizer_access_token |
string |
授权方令牌 |
expires_in |
nubmer 有效期,单位:秒 |
|
authorizer_refresh_token |
string |
刷新令牌 |
返回结果示例:
{
"authorizer_access_token": "some-access-token",
"expires_in": 7200,
"authorizer_refresh_token": "refresh_token_value"
}