1
0
mirror of synced 2025-12-23 18:48:00 +08:00

🐛 #2200 【小程序】修复获取AccessToken时ApiHostUrl未生效的问题

This commit is contained in:
tangliu
2021-07-13 10:00:26 +08:00
committed by GitHub
parent 2d2cf39f1c
commit 737d75964b
3 changed files with 19 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
@@ -60,7 +61,12 @@ public class WxMaServiceHttpClientImpl extends BaseWxMaServiceImpl {
@Override @Override
protected String doGetAccessTokenRequest() throws IOException { protected String doGetAccessTokenRequest() throws IOException {
String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
WxMaService.GET_ACCESS_TOKEN_URL;
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
HttpGet httpGet = null; HttpGet httpGet = null;
CloseableHttpResponse response = null; CloseableHttpResponse response = null;

View File

@@ -7,6 +7,7 @@ import jodd.http.HttpRequest;
import jodd.http.ProxyInfo; import jodd.http.ProxyInfo;
import jodd.http.net.SocketHttpConnectionProvider; import jodd.http.net.SocketHttpConnectionProvider;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException; import java.io.IOException;
@@ -45,7 +46,11 @@ public class WxMaServiceJoddHttpImpl extends BaseWxMaServiceImpl<HttpConnectionP
@Override @Override
protected String doGetAccessTokenRequest() throws IOException { protected String doGetAccessTokenRequest() throws IOException {
String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret()); String url = StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
WxMaService.GET_ACCESS_TOKEN_URL;
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
HttpRequest request = HttpRequest.get(url); HttpRequest request = HttpRequest.get(url);
if (this.getRequestHttpProxy() != null) { if (this.getRequestHttpProxy() != null) {
SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider();

View File

@@ -5,6 +5,7 @@ import cn.binarywang.wx.miniapp.config.WxMaConfig;
import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
import okhttp3.*; import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
@@ -63,7 +64,11 @@ public class WxMaServiceOkHttpImpl extends BaseWxMaServiceImpl<OkHttpClient, OkH
@Override @Override
protected String doGetAccessTokenRequest() throws IOException { protected String doGetAccessTokenRequest() throws IOException {
String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret()); String url = StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
WxMaService.GET_ACCESS_TOKEN_URL;
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
Request request = new Request.Builder().url(url).get().build(); Request request = new Request.Builder().url(url).get().build();
try (Response response = getRequestHttpClient().newCall(request).execute()) { try (Response response = getRequestHttpClient().newCall(request).execute()) {
return Objects.requireNonNull(response.body()).string(); return Objects.requireNonNull(response.body()).string();