获取微信公众号用户列表及用户详情信息
一、获取有效的访问令牌accessToken
1. 使用公众号的AppID和AppSecret通过网络请求获取access_token,建议将access_token缓存起来以便重复使用。
2. access_token的有效期是7200秒(两小时),在有效期内,可以一直使用,只有当access_token过期时,才需要再次调用接口 获取access_token。在理想情况下,一个7x24小时运行的系统,每天只需要获取12次access_token,即每2小时获取一次。如果在 有效期内,再次获取access_token,那么上一次获取的access_token将失效。
3. 微信公众平台接口调用频次图:
(1) 新注册的账号
(2) 测试号申请页中申请的测试号
4. 获取方法:
/**
* 获取access_token,存进redis,1小时50分获取一次,避免频繁调用接口
*/
public String getAccessToken() {
Object redisAccessToken = redisUtil.get("redisAccessToken");
System.out.println(redisAccessToken);
if(oConvertUtils.isEmpty(redisAccessToken)){
RestTemplate restTemplate = new RestTemplate();
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + Configure.appID_gzh + "&secret=" + Configure.secret_gzh;
String response = restTemplate.getForObject(url, String.class);
JSONObject object = JSONObject.parseObject(response);
String accessToken = object.getString("access_token");
redisUtil.set("redisAccessToken",accessToken,6600);
return accessToken;
}else{
return redisAccessToken.toString();
}
}
二、获取关注公众号的用户(只有用户的公众号openId)
1.获取单页(可以测试,学习用)
/**
* @Description: 获取关注公众号的用户
*/
public JSONArray getGzhUser() throws IOException {
String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken + "&next_openid=";
String s = HttpRequest.connectionGet(url);
JSONObject jsonObject = JSONObject.parseObject(s);
return jsonObject;
}
2.循环获取所有的用户列表
/**
* @Description: 获取关注公众号的用户
*/
public JSONArray getGzhUser() throws IOException {
//while循环获取全部用户
String nextOpenid = null;
JSONArray allJsonArray = new JSONArray();
while (true){
String nextUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken;
if(null != nextOpenid){
nextUrl += "&next_openid=" + nextOpenid;
}
String nextS = HttpRequest.connectionGet(nextUrl);
JSONObject jsonObject = JSONObject.parseObject(nextS);
nextOpenid = jsonObject.getString("next_openid");
if(jsonObject.size() > 3){
JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("openid");
for (Object gzhOpenid : jsonArray) {
allJsonArray.add(gzhOpenid);
}
}
if(null == nextOpenid || oConvertUtils.isEmpty(nextOpenid)){
break;
}
}
return allJsonArray;
}
三、获取公众号用户详情信息
详情信息里包含unionId: 可通过 UnionID 来区分用户的唯一性,因为只要是同一个微信开放平台帐号下的移动应用、网站应用和公众帐号(包括小程序),用户的 UnionID 是唯一的。换句话说,同一用户,对同一个微信开放平台下的不同应用,UnionID是相同的。
官方关于unionid原文档链接
/**
* @Description: 获取公众号详情
*/
public JSONObject getGzhUserDetail(String openid) throws IOException {
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&lang=zh_CN" + "&openid=" + openid;
String s = HttpRequest.connectionGet(url);
JSONObject jsonObject = JSONObject.parseObject(s);
return jsonObject;
}
四、请求工具类
public static String connectionGet(String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
StringBuilder result = new StringBuilder();
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
}
注:小程序用户登录时,调用接口获取到unionId,小程序和公众号绑定后,可通过unionId去互相匹配。
一个在学习的开发者,勿喷,欢迎交流