微信公众号获取已发布的文章信息

获取 Access token

获取 Access token

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

// 换成自己的公众号 AppID
private static final String APP_ID = "";

// 换成自己的公众号 AppSecret
private static final String APP_SECRET = "";

private static final String API_URL = "https://api.weixin.qq.com/cgi-bin/";

// https请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
private static final String ACCESS_TOKEN_URL = API_URL + "token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + APP_SECRET;

/**
* 获取 Access token
*/
public static String getAccessToken() {
String response = HttpUtil.get(ACCESS_TOKEN_URL);
JSONObject json = new JSONObject(response);
return json.getStr("access_token");
}

获取成功发布列表

获取成功发布列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// http 请求方式:POST(请使用https协议)https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=ACCESS_TOKEN
private static final String ARTICLELiST_URL = API_URL + "freepublish/batchget?access_token=";

/**
* 获取成功发布列表
*/
public static JSONArray getArticles(String accessToken) {
JSONObject params = new JSONObject();
params.put("offset", 0);
params.put("count", 20);
String response = HttpUtil.post(ARTICLELiST_URL + accessToken, params.toString());
JSONObject json = new JSONObject(response);
return json.getJSONArray("item");
}