一、接口说明
公众号可通过本接口来获取帐号的关注者列表,关注者列表由一串OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的)组成。一次拉取调用最多拉取10000个关注者的OpenID,可以通过多次拉取的方式来满足需求。
二、接口调用
1 接口调用请求说明
(1)http请求方式: GET(请使用https协议)
https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
(2)请求参数
参数 是否必须 说明
access_token 是 调用接口凭证
next_openid 是 第一个拉取的OPENID,不填默认从头开始拉取
2 接口调用返回说明
(1)正确时返回JSON数据包
{"total":2,"count":2,"data":{"openid":["","OPENID1","OPENID2"]},"next_openid":"NEXT_OPENID"}
(2)返回参数
参数 说明
total 关注该公众账号的总用户数
count 拉取的OPENID个数,最大值为10000
data 列表数据,OPENID的列表
next_openid 拉取列表的最后一个用户的OPENID
附:关注者数量超过10000时
当公众号关注者数量超过10000时,可通过填写next_openid的值,从而多次拉取列表的方式来满足需求。
具体而言,就是在调用接口时,将上一次调用得到的返回中的next_openid值,作为下一次调用中的next_openid值。
示例如下:
公众账号A拥有23000个关注的人,想通过拉取关注接口获取所有关注的人,那么分别请求url如下:https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN 返回结果:
{ "total":23000, "count":10000, "data":{" openid":[ "OPENID1", "OPENID2", ..., "OPENID10000" ] }, "next_openid":"OPENID10000" }https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID1返回结果: { "total":23000, "count":10000, "data":{ "openid":[ "OPENID10001", "OPENID10002", ..., "OPENID20000" ] }, "next_openid":"OPENID20000" }https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID2返回结果(关注者列表已返回完时,返回next_openid为空): { "total":23000, "count":3000, "data":{" "openid":[ "OPENID20001", "OPENID20002", ..., "OPENID23000" ] }, "next_openid":"OPENID23000" }
三、java接口开发
注:此接口开发使用spring的RestTemplate方法进行http请求,如果不使用此方法可以使用其他http请求工具或方法进行http的请求。此方法仅供参考!
1 返回参数对象WeixinUserList
/** * 类描述:微信公众平台用户openId列表信息 * 开发人员:wangqiulin * 开发时间:2017-9-5 */ public class WeixinUserList { private Integer total;//关注该公众账号的总用户数 private Integer count;//拉取的OPENID个数,最大值为10000 private WxOpenidInfo data;//列表数据,OPENID的列表 private String next_openid;//拉取列表的最后一个用户的OPENID private int errcode;//错误编码 private String errmsg="ok";//错误提示 public Integer getTotal() { return total; } public void setTotal(Integer total) { this.total = total; } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } public String getNext_openid() { return next_openid; } public void setNext_openid(String next_openid) { this.next_openid = next_openid; } public WxOpenidInfo getData() { return data; } public void setData(WxOpenidInfo data) { this.data = data; } public int getErrcode() { return errcode; } public void setErrcode(int errcode) { this.errcode = errcode; } public String getErrmsg() { return errmsg; } public void setErrmsg(String errmsg) { this.errmsg = errmsg; } } openidList集合对象 import java.util.List; public class WxOpenidInfo { private List<string> openid; public List<string> getOpenid() { return openid; } public void setOpenid(List<string> openid) { this.openid = openid; } }
2 接口方法
将获取到的openid集合写入txt文件,写入数据库也类似
public WeixinUserList getUserOpenIdList(String nextOpenid, String accessToken) { //用户openid列表信息 WeixinUserList openIdListInfo = null; synchronized(this){ try { //循环获取用户openid列表 do{ //微信公众号获取用户列表信息接口地址 String requestUrl = null; if(StringUtils.isBlank(nextOpenid)){ requestUrl = new StringBuffer().append("https://api.weixin.qq.com/cgi-bin/user/get?access_token=").append(accessToken).toString(); }else { requestUrl = new StringBuffer().append("https://api.weixin.qq.com/cgi-bin/user/get?access_token=") .append(accessToken).append("&next_openid=").append(nextOpenid).toString(); } openIdListInfo = restTemplate.getForObject(requestUrl, WeixinUserList.class); if(openIdListInfo != null && openIdListInfo.getErrcode() == 0){ //获取用户openid列表对象 WxOpenidInfo wxOpenidInfo = openIdListInfo.getData(); if(wxOpenidInfo != null){ List<string> openids = wxOpenidInfo.getOpenid(); if(openids != null && openids.size() > 0){ //生成数据的表头 StringBuffer text = new StringBuffer(); for (String openid : openids) { //生成数据的内容 text.append(openid+" "); } //写入txt文件中 writeTxtFile(text.toString()); } //拉取列表的最后一个用户的OPENID nextOpenid = openIdListInfo.getNext_openid(); } }else { openIdListInfo.setErrcode(40000); openIdListInfo.setErrmsg("获取关注用户列表失败"); return openIdListInfo ; } } while (openIdListInfo.getCount() == 10000); } catch (Exception e) { LOG.error("获取用户列表失败",e); openIdListInfo .setErrcode(40000); openIdListInfo .setErrmsg("获取用户列表失败"); return openIdListInfo ; } } return openIdListInfo; }
将获取的openid列表写入txt文件
/** * 写文件 */ public void writeTxtFile(String content) throws IOException{ //指定文件路径和名称 String path = "D:/openidList.txt"; File filename = new File(path); if (!filename.exists()) { filename.createNewFile(); LOG.info(filename + "已创建!"); } //先读取原有文件内容,然后进行写入操作 RandomAccessFile randomAccessFile = null; try { randomAccessFile = new RandomAccessFile(filename, "rw"); // 文件长度,字节数 long fileLength = randomAccessFile.length(); // 将写文件指针移到文件尾。 randomAccessFile.seek(fileLength); randomAccessFile.writeBytes(content); } catch (IOException e1) {
e1.printStackTrace();
} finally {
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}