获取微信accessToken

来源:互联网 发布:微信能听的唱歌软件 编辑:程序博客网 时间:2024/05/20 21:18

//调用微信接口

//appid:公众号appid 

//secret:公众号appid密钥

String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx039fa5e93cf51b8e&secret=e2fa4c2a05ce99575e6c139a5c37de1c";

JSONObject jsonObject = doGetStr(url);

//accesstoken 值

accessToken = jsonObject .getString("access_token");



/**
* Http Get 请求方法
* @param url
* @return
* @throws Exception
*/
public static JSONObject doGet(String url) throws Exception {


JSONObject json = null;
HttpGet httpGet = new HttpGet(url);
// 创建httpClientBuilder
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
// httpclient
CloseableHttpClient httpClient = clientBuilder.build();


try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity, "UTF-8");
json = JSONObject.fromObject(result);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new RecruitException(ErrorsInfo.ERR00001, "HttpClient 请求异常", "HttpClient 请求异常,请检查日志!");
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new RecruitException(ErrorsInfo.ERR00002, "IO 读取异常", "IO 读取异常,请检查日志!");
}


return json;
}







阅读全文
0 0