Skip to content

Commit 737d759

Browse files
🐛 #2200 【小程序】修复获取AccessToken时ApiHostUrl未生效的问题
1 parent 2d2cf39 commit 737d759

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceHttpClientImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import me.chanjar.weixin.common.util.http.HttpType;
77
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
88
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
9+
import org.apache.commons.lang3.StringUtils;
910
import org.apache.http.HttpHost;
1011
import org.apache.http.client.config.RequestConfig;
1112
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -60,7 +61,12 @@ public HttpType getRequestType() {
6061

6162
@Override
6263
protected String doGetAccessTokenRequest() throws IOException {
63-
String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
64+
65+
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
66+
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
67+
WxMaService.GET_ACCESS_TOKEN_URL;
68+
69+
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
6470

6571
HttpGet httpGet = null;
6672
CloseableHttpResponse response = null;

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceJoddHttpImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import jodd.http.ProxyInfo;
88
import jodd.http.net.SocketHttpConnectionProvider;
99
import me.chanjar.weixin.common.util.http.HttpType;
10+
import org.apache.commons.lang3.StringUtils;
1011

1112
import java.io.IOException;
1213

@@ -45,7 +46,11 @@ public HttpType getRequestType() {
4546

4647
@Override
4748
protected String doGetAccessTokenRequest() throws IOException {
48-
String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
49+
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
50+
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
51+
WxMaService.GET_ACCESS_TOKEN_URL;
52+
53+
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
4954
HttpRequest request = HttpRequest.get(url);
5055
if (this.getRequestHttpProxy() != null) {
5156
SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider();

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceOkHttpImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import me.chanjar.weixin.common.util.http.HttpType;
66
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
77
import okhttp3.*;
8+
import org.apache.commons.lang3.StringUtils;
89

910
import java.io.IOException;
1011
import java.util.Objects;
@@ -63,7 +64,11 @@ public HttpType getRequestType() {
6364

6465
@Override
6566
protected String doGetAccessTokenRequest() throws IOException {
66-
String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
67+
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
68+
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
69+
WxMaService.GET_ACCESS_TOKEN_URL;
70+
71+
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
6772
Request request = new Request.Builder().url(url).get().build();
6873
try (Response response = getRequestHttpClient().newCall(request).execute()) {
6974
return Objects.requireNonNull(response.body()).string();

0 commit comments

Comments
 (0)