代码如下:
WxMaService wxMaService = new WxMaServiceImpl();
WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
wxMaConfig.setAppid(appid);
wxMaConfig.setSecret(secret);
if (getAppletHttpProxy()) {
wxMaConfig.setHttpProxyHost(PROXY_HOST);
wxMaConfig.setHttpProxyPort(PROXY_PORT);
}
wxMaConfig.setMsgDataFormat("JSON");
wxMaService.setWxMaConfig(wxMaConfig);
WxMaJscode2SessionResult wxMaJscode2SessionResult = null;
try {
LOGGER.info("wxMaConfig=={}",wxMaService.getWxMaConfig().toString());
wxMaJscode2SessionResult = wxMaService.jsCode2SessionInfo(bo.getCode());
} catch (WxErrorException e){
e.printStackTrace();
WxError error = e.getError();
LOGGER.error("获取accessToken失败,errCode=[{}]errMsg=[{}]", error.getErrorCode(), error.getErrorMsg());
throw new BizException(String.valueOf(error.getErrorCode()), error.getErrorMsg());
}
调用小程序weixin-java-miniapp包底层代码WxMaServiceImpl
Lock lock = getWxMaConfig().getAccessTokenLock();
lock.lock();
String var10;
try {
String url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
getWxMaConfig().getAppid(),getWxMaConfig().getSecret());
try {
HttpGet httpGet = new HttpGet(url);
LOGGER.info("httpGet=={}",httpGet.toString()); // httpGet==GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx280f80408593d848&secret=19b56acbe67fdad678249210f8fc4526 HTTP/1.1
if (getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(getRequestHttpProxy()).build();
LOGGER.info("config=={}",config.toString()); // config==[expectContinueEnabled=false, proxy=http://10.16.126.30:2443, localAddress=null, cookieSpec=null, redirectsEnabled=true, relativeRedirectsAllowed=true, maxRedirects=50, circularRedirectsAllowed=false, authenticationEnabled=true, targetPreferredAuthSchemes=null, proxyPreferredAuthSchemes=null, connectionRequestTimeout=-1, connectTimeout=-1, socketTimeout=-1, contentCompressionEnabled=true, normalizeUri=true]
httpGet.setConfig(config);
}
try {
CloseableHttpResponse response = getRequestHttpClient().execute(httpGet);
LOGGER.info("response=={}",response.toString()); //response==HttpResponseProxy{HTTP/1.1 400 Bad Request [Server: nginx, Date: Thu, 25 Aug 2022 01:18:57 GMT, Content-Type: text/html, Content-Length: 150, Connection: close] org.apache.http.entity.BufferedHttpEntity@32ab3ee7}
Throwable var6 = null;
由于应用服务器A无法与外网通信,需要应用服务器A通过代理前置服务器B去访问外网 B服务器地址为:10.16.126.30:3443
前置服务器nginx配置,还有一个疑问,location /weixin 这个地址在代码中哪里配置?
nginx配置 location / { 报错还是一样
下面是上边的代码打印的日志
httpGet==GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx280f80408593d848&secret=19b56acbe67fdad678249210f8fc4526 HTTP/1.1
config==[expectContinueEnabled=false, proxy=http://10.16.126.30:3443, localAddress=null, cookieSpec=null, redirectsEnabled=true, relativeRedirectsAllowed=true, maxRedirects=50, circularRedirectsAllowed=false, authenticationEnabled=true, targetPreferredAuthSchemes=null, proxyPreferredAuthSchemes=null, connectionRequestTimeout=-1, connectTimeout=-1, socketTimeout=-1, contentCompressionEnabled=true, normalizeUri=true]
response==HttpResponseProxy{HTTP/1.1 400 Bad Request [Server: nginx, Date: Thu, 25 Aug 2022 01:18:57 GMT, Content-Type: text/html, Content-Length: 150, Connection: close] org.apache.http.entity.BufferedHttpEntity@32ab3ee7}
这是前置服务器打印的日志
-" "-" "[25/Aug/2022:09:18:57 +0800]" "CONNECT api.weixin.qq.com:443 HTTP/1.1" "400" "150" "-" "-" "10.16.98.50" "-" "-" "0.001" "-" "-"
应用服务器A访问下面这个地址是可以获得token的
curl “http://10.16.126.30:3443/weixin/cgi-bin/token?grant_type=client_credential&appid=wx280f80408593d848&secret=19b56acbe67fdad678249210f8fc4526”
curl调通了 再讲代码吧