Commit 3bc0798f authored by shiyu's avatar shiyu

公众号刷新token方式修改

parent ff854114
...@@ -20,7 +20,7 @@ dts: ...@@ -20,7 +20,7 @@ dts:
officialaccount-secret: daecb3de53841fe1a787bc9849ac3406 officialaccount-secret: daecb3de53841fe1a787bc9849ac3406
officialaccount-token: Uu6GHYOiP1xJ6GaBMyXfnITufAGJTe3Q officialaccount-token: Uu6GHYOiP1xJ6GaBMyXfnITufAGJTe3Q
officialaccount-key: noeN1amYRvzrVmE2GnC5VnsIfgPEmv6bhX54wJOARlM officialaccount-key: noeN1amYRvzrVmE2GnC5VnsIfgPEmv6bhX54wJOARlM
get-officialaccount-accesstoken-url: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential get-officialaccount-accesstoken-url: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s
#腾讯IM配置 #腾讯IM配置
im: im:
......
...@@ -39,7 +39,7 @@ public class OfficialAccountApi { ...@@ -39,7 +39,7 @@ public class OfficialAccountApi {
private String OFFICIAL_ACCOUNT_APPID; private String OFFICIAL_ACCOUNT_APPID;
@Value("${dts.wx.officialaccount-secret}") @Value("${dts.wx.officialaccount-secret}")
private String APPSECRET; private String OFFICIAL_ACCOUNT_APPSECRET;
@Value("${dts.wx.app-id}") @Value("${dts.wx.app-id}")
private String APPLET_APPID; private String APPLET_APPID;
...@@ -47,8 +47,6 @@ public class OfficialAccountApi { ...@@ -47,8 +47,6 @@ public class OfficialAccountApi {
@Value("${dts.wx.msg-url}") @Value("${dts.wx.msg-url}")
private String MSG_URL; private String MSG_URL;
@Autowired @Autowired
RedisUtils redisUtils; RedisUtils redisUtils;
...@@ -56,6 +54,15 @@ public class OfficialAccountApi { ...@@ -56,6 +54,15 @@ public class OfficialAccountApi {
WxLoginManager wxLoginManager; WxLoginManager wxLoginManager;
public String getAccessToken() { public String getAccessToken() {
//测试环境
if (!GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL.contains("stable_token")) {
String accessTokenUrl = String.format(GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL, OFFICIAL_ACCOUNT_APPID, OFFICIAL_ACCOUNT_APPSECRET);
String res = OkHttpUtil.builder().url(accessTokenUrl)
.get().sync();
String token = JsonUtils.getValueByPath(res, "access_token");
return token;
}
//正式环境先从缓存中获取,没有再刷新token
//根据商户的appid从redis中获取商户的token //根据商户的appid从redis中获取商户的token
String token = redisUtils.hasKey(OFFICIAL_ACCOUNT_APPID)? redisUtils.get(OFFICIAL_ACCOUNT_APPID).toString() : null; String token = redisUtils.hasKey(OFFICIAL_ACCOUNT_APPID)? redisUtils.get(OFFICIAL_ACCOUNT_APPID).toString() : null;
if (StringUtils.isEmpty(token)) { if (StringUtils.isEmpty(token)) {
...@@ -65,21 +72,40 @@ public class OfficialAccountApi { ...@@ -65,21 +72,40 @@ public class OfficialAccountApi {
} }
public String refreshAccessToken() { public String refreshAccessToken() {
String url = GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL + "&appid=" + OFFICIAL_ACCOUNT_APPID + "&secret=" + APPSECRET; try {
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url); String accessTokenUrl = null;
okHttpUtil.get(); String responseStr = null;
String responseStr = okHttpUtil.async(); //正式环境的url中包含了"stable_token", 要使用post请求, 测试环境是get请求
logger.info("appid : {}, 获取token response :{}", OFFICIAL_ACCOUNT_APPID, responseStr); if (!GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL.contains("stable_token")) {
accessTokenUrl = String.format(GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL, OFFICIAL_ACCOUNT_APPID, OFFICIAL_ACCOUNT_APPSECRET);
logger.info("测试环境获取公众号access_token url :{}", accessTokenUrl);
responseStr = OkHttpUtil.builder().url(accessTokenUrl)
.get().sync();
} else {
accessTokenUrl = GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL;
logger.info("正式环境获取公众号access_token url :{}", accessTokenUrl);
Map<String, Object> map = new HashMap<>();
map.put("grant_type", "client_credential");
map.put("appid", OFFICIAL_ACCOUNT_APPID);
map.put("secret", OFFICIAL_ACCOUNT_APPSECRET);
responseStr = OkHttpUtil.builder().addParams(map).url(accessTokenUrl)
.post(true).sync();
}
logger.info("公众号刷新access_token response :{}", responseStr);
if (!responseStr.contains("access_token")) { if (!responseStr.contains("access_token")) {
String errorCode = JsonUtils.getValueByPath(responseStr, "errcode"); String errorCode = JsonUtils.getValueByPath(responseStr, "errcode");
String errmsg = JsonUtils.getValueByPath(responseStr, "errmsg"); String errmsg = JsonUtils.getValueByPath(responseStr, "errmsg");
logger.error("appid : {}, 获取token出错, errorCode : {}, errmsg : {} ", OFFICIAL_ACCOUNT_APPID, errorCode, errmsg); logger.error("appid : {}, 公众号获取token出错, errorCode : {}, errmsg : {} ", OFFICIAL_ACCOUNT_APPID, errorCode, errmsg);
return null; return null;
} }
String token = JsonUtils.getValueByPath(responseStr, "access_token"); String token = JsonUtils.getValueByPath(responseStr, "access_token");
String expires_in = JsonUtils.getValueByPath(responseStr, "expires_in"); String expires_in = JsonUtils.getValueByPath(responseStr, "expires_in");
redisUtils.set(OFFICIAL_ACCOUNT_APPID, token, Long.valueOf(expires_in) - 10, TimeUnit.SECONDS); redisUtils.set(OFFICIAL_ACCOUNT_APPID, token, Long.valueOf(expires_in) - 10, TimeUnit.SECONDS);
return token; return token;
} catch (Exception e) {
logger.error("获取公众号access_token失败", e);
return null;
}
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment