Parcourir la source

将wxToken 调整新的稳健版url redis缓存时间1800s

hxd il y a 1 mois
Parent
commit
141409aea3

+ 13 - 6
portal-service-domain/src/main/java/com/hfln/portal/domain/customer/util/WxOfficeAccountClient.java

@@ -10,6 +10,7 @@ import org.apache.http.HttpEntity;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
@@ -116,14 +117,20 @@ public class WxOfficeAccountClient {
             return cachedToken;
         }
         //2.如果Redis中没有,则调用微信接口获取
-        String url = "https://api.weixin.qq.com/cgi-bin/stable_token" +
-                "?grant_type=client_credential" +
-                "&appid=" + appId +
-                "&secret=" + secret;
+        String url = "https://api.weixin.qq.com/cgi-bin/stable_token";
 
         try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
-            HttpGet httpGet = new HttpGet(url);
-            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
+            HttpPost httpPost = new HttpPost(url);
+
+            String jsonBody = String.format(
+                    "{\"grant_type\": \"client_credential\", \"appid\": \"%s\", \"secret\": \"%s\"}",
+                    appId, secret
+            );
+
+            StringEntity requestEntity = new StringEntity(jsonBody, ContentType.APPLICATION_JSON);
+            httpPost.setEntity(requestEntity);
+
+            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                 HttpEntity entity = response.getEntity();
                 String result = EntityUtils.toString(entity);
                 Map<String, Object> map = gson.fromJson(result, Map.class);