微信获取access_token

原创
04/26 16:46
阅读数 0
public static void main(String[] args) {
    String screet="79f60b0947df914f6f8c78c71b3f";
    String appid="wx1868a8216c548";
    System.out.println(getAccessToken(screet,appid));
}

public static String getAccessToken(String screet,String appid) {
    String accesstoken = "";
    //创建HttpClientBuilder
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    //HttpClient
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

    HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + screet);
    System.out.println(httpGet.getRequestLine());
    try {
        //执行get请求
        HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
        //获取响应消 息实体
        HttpEntity entity = httpResponse.getEntity();
        //响应状态
        System.out.println("status:" + httpResponse.getStatusLine());
        //判断响应实体是否为空
        if (entity != null) {
            String entityString = EntityUtils.toString(entity);
            JSON parse = (JSON) JSON.parse(entityString);
            AccessToken accessToken = JSON.toJavaObject(parse, AccessToken.class);
            accesstoken = accessToken.getAccess_token().toString();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return accesstoken;
}
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部