Skip to content

Commit 67a846b

Browse files
committed
出现40014的access_token问题时需要自动刷新token binarywang#197
1 parent 19b3b99 commit 67a846b

File tree

5 files changed

+31
-39
lines changed

5 files changed

+31
-39
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/AbstractWxCpService.java renamed to weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/AbstractWxCpServiceImpl.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3-
import java.io.File;
4-
import java.io.IOException;
5-
import java.io.InputStream;
6-
import java.util.List;
7-
import java.util.UUID;
8-
9-
import org.apache.commons.lang3.StringUtils;
10-
import org.slf4j.Logger;
11-
import org.slf4j.LoggerFactory;
12-
133
import com.google.gson.*;
144
import com.google.gson.reflect.TypeToken;
15-
165
import me.chanjar.weixin.common.bean.WxJsapiSignature;
176
import me.chanjar.weixin.common.bean.menu.WxMenu;
187
import me.chanjar.weixin.common.bean.result.WxError;
@@ -33,10 +22,19 @@
3322
import me.chanjar.weixin.cp.bean.WxCpTag;
3423
import me.chanjar.weixin.cp.bean.WxCpUser;
3524
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
25+
import org.apache.commons.lang3.StringUtils;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import java.io.File;
30+
import java.io.IOException;
31+
import java.io.InputStream;
32+
import java.util.List;
33+
import java.util.UUID;
3634

37-
public abstract class AbstractWxCpService<H, P> implements WxCpService, RequestHttp<H, P> {
35+
public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, RequestHttp<H, P> {
3836

39-
protected final Logger log = LoggerFactory.getLogger(AbstractWxCpService.class);
37+
protected final Logger log = LoggerFactory.getLogger(AbstractWxCpServiceImpl.class);
4038

4139
/**
4240
* 全局的是否正在刷新access token的锁
@@ -493,9 +491,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
493491
int retryTimes = 0;
494492
do {
495493
try {
496-
T result = this.executeInternal(executor, uri, data);
497-
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}",uri, data, result);
498-
return result;
494+
return this.executeInternal(executor, uri, data);
499495
} catch (WxErrorException e) {
500496
if (retryTimes + 1 > this.maxRetryTimes) {
501497
this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
@@ -525,37 +521,39 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
525521
throw new RuntimeException("微信服务端异常,超出重试次数");
526522
}
527523

528-
public synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
524+
private synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
529525
if (uri.contains("access_token=")) {
530526
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
531527
}
532528
String accessToken = getAccessToken(false);
533529

534-
String uriWithAccessToken = uri;
535-
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
530+
String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken;
536531

537532
try {
538-
return executor.execute(this, uriWithAccessToken, data);
533+
T result = executor.execute(this, uriWithAccessToken, data);
534+
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uriWithAccessToken, data, result);
535+
return result;
539536
} catch (WxErrorException e) {
540537
WxError error = e.getError();
541538
/*
542539
* 发生以下情况时尝试刷新access_token
543540
* 40001 获取access_token时AppSecret错误,或者access_token无效
544541
* 42001 access_token超时
542+
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口
545543
*/
546-
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
544+
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) {
547545
// 强制设置wxCpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
548546
this.configStorage.expireAccessToken();
549547
return execute(executor, uri, data);
550548
}
551549

552550
if (error.getErrorCode() != 0) {
553-
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data, error);
551+
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uriWithAccessToken, data, error);
554552
throw new WxErrorException(error);
555553
}
556554
return null;
557555
} catch (IOException e) {
558-
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", uri, data, e.getMessage());
556+
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", uriWithAccessToken, data, e.getMessage());
559557
throw new RuntimeException(e);
560558
}
561559
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/apache/WxCpServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
88
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
99
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
10-
import me.chanjar.weixin.cp.api.impl.AbstractWxCpService;
10+
import me.chanjar.weixin.cp.api.impl.AbstractWxCpServiceImpl;
1111

1212
import org.apache.http.HttpHost;
1313
import org.apache.http.client.config.RequestConfig;
@@ -18,7 +18,7 @@
1818

1919
import java.io.IOException;
2020

21-
public class WxCpServiceImpl extends AbstractWxCpService<CloseableHttpClient, HttpHost> {
21+
public class WxCpServiceImpl extends AbstractWxCpServiceImpl<CloseableHttpClient, HttpHost> {
2222
protected CloseableHttpClient httpClient;
2323
protected HttpHost httpProxy;
2424

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/jodd/WxCpServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import me.chanjar.weixin.common.bean.result.WxError;
66
import me.chanjar.weixin.common.exception.WxErrorException;
77
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
8-
import me.chanjar.weixin.cp.api.impl.AbstractWxCpService;
8+
import me.chanjar.weixin.cp.api.impl.AbstractWxCpServiceImpl;
99

10-
public class WxCpServiceImpl extends AbstractWxCpService<HttpConnectionProvider, ProxyInfo> {
10+
public class WxCpServiceImpl extends AbstractWxCpServiceImpl<HttpConnectionProvider, ProxyInfo> {
1111
protected HttpConnectionProvider httpClient;
1212
protected ProxyInfo httpProxy;
1313

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/okhttp/WxCpServiceImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
import java.io.IOException;
44

5-
import jodd.http.*;
65
import me.chanjar.weixin.common.bean.WxAccessToken;
76
import me.chanjar.weixin.common.bean.result.WxError;
87
import me.chanjar.weixin.common.exception.WxErrorException;
98
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
109
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
11-
import me.chanjar.weixin.cp.api.impl.AbstractWxCpService;
10+
import me.chanjar.weixin.cp.api.impl.AbstractWxCpServiceImpl;
1211
import okhttp3.*;
1312

14-
public class WxCpServiceImpl extends AbstractWxCpService<ConnectionPool, OkhttpProxyInfo> {
13+
public class WxCpServiceImpl extends AbstractWxCpServiceImpl<ConnectionPool, OkhttpProxyInfo> {
1514
protected ConnectionPool httpClient;
1615
protected OkhttpProxyInfo httpProxy;
1716

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/AbstractWxMpServiceImpl.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
292292
int retryTimes = 0;
293293
do {
294294
try {
295-
T result = executeInternal(executor, uri, data);
296-
return result;
295+
return this.executeInternal(executor, uri, data);
297296
} catch (WxErrorException e) {
298297
if (retryTimes + 1 > this.maxRetryTimes) {
299298
this.log.warn("重试达到最大次数【{}】", maxRetryTimes);
@@ -327,12 +326,7 @@ public synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, Str
327326
}
328327
String accessToken = getAccessToken(false);
329328

330-
String uriWithAccessToken;
331-
if (uri.contains("?")) {
332-
uriWithAccessToken = uri + "&access_token=" + accessToken;
333-
} else {
334-
uriWithAccessToken = uri + "?access_token=" + accessToken;
335-
}
329+
String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken;
336330

337331
try {
338332
T result = executor.execute(this, uriWithAccessToken, data);
@@ -344,8 +338,9 @@ public synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, Str
344338
* 发生以下情况时尝试刷新access_token
345339
* 40001 获取access_token时AppSecret错误,或者access_token无效
346340
* 42001 access_token超时
341+
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口
347342
*/
348-
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
343+
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) {
349344
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
350345
this.getWxMpConfigStorage().expireAccessToken();
351346
if (this.getWxMpConfigStorage().autoRefreshToken()) {

0 commit comments

Comments
 (0)