一、接口說明
公眾號可通過本接口來獲取帳號的關注者列表,關注者列表由一串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+"\r\n"); } //寫入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();
}
}
}
}