private static final CloseableHttpClient httpclient = HttpClients.createDefault();
private static final String appid = "你的appid";
private static final String secret = "你的secret";
public static String getAccessToken() {
String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+ appid
+ "&secret="
+ secret;
String result = HttpUtil.sendGet(url);
JSONObject object = JSONObject.fromObject(result);
String Access_Token = object.getString("access_token");
return Access_Token;
}
public static String sendGet(String url) {
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpget);
} catch (IOException e1) {
e1.printStackTrace();
}
String result = null;
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
小程序获取Access_Token
最新推荐文章于 2024-07-05 01:12:33 发布