場景,獲取微信服務號/訂閱號粉絲列表,微信提供API每次最多返回10000條;要想獲取全部粉絲列表,需要手動的調用N次實現;
解決方案:遞歸調用,獲取全部粉絲列表
直接上代碼:
獲取全部粉絲列表調用:
// 遞歸獲取全部粉絲列表
public void getAll(){
long start = System.currentTimeMillis();
// 獲取訂閱號token
String sub_token = fetchToken(SUB_APP_ID, SUB_APP_SECRET);
getUserList(sub_token,"");
long end = System.currentTimeMillis()-start;
logger.info("執行時間:"+end+"ms");
}
getUserList方法:
// 遞歸調用next_openid
private void getUserList(String sub_token, String next_openid) {
HttpMethod method = null;
String res = null;
String GetUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+sub_token+"&next_openid="+next_openid; // +"&next_openid=o_GmSwimbNwkk9ca9ogsRCClzeUg"
method = new GetMethod(GetUrl);
try {
res = HttpClientUtil.httpRequest(method);
if (!"none".equals(res)) {
JSONObject jsonobj = JSON.parseObject(res);
String nextOpen = jsonobj.getString("next_openid");
logger.info("start——next_openid:" + nextOpen);
JSONObject jo = (JSONObject) jsonobj.get("data");
JSONArray jsonArray = JSON.parseArray(jo.getString("openid"));
if (null != jo && null != jsonArray) {
// 獲取服務號token
logger.info("========================================================================");
StringBuffer sb = new StringBuffer();
sb.append("INSERT INTO `wx_sub` VALUES ");
for (int i = 0; i < jsonArray.size(); i++) {
String openId = (String) jsonArray.get(i);
if(i==jsonArray.size()-1){
sb.append("(UUID(),'"+openId+"','"+i+"',now());");
}else {
sb.append("(UUID(),'"+openId+"','"+i+"',now()),");
}
// 通過openId獲取unionId
// getUserInfo(sub_token,openId);
}
logger.info(sb);
logger.info("========================================================================");
}
logger.info("end——next_openid:" + nextOpen);
// 繼續獲取
if(StringUtils.isNotEmpty(nextOpen)){
getUserList(sub_token,nextOpen);
}
}
} catch (IOException e) {
System.out.print("IOException");
} catch (Exception e) {
System.out.print("Exception");
}
}
獲取token方法:token有效期-2個小時
/**
* @Title: fetchToken
* @Description: 獲取微信的token
* @return 返回結果JSONOjbect對象:{"access_token":"","expires_in":7200}
*/
protected String fetchToken(String appid,String appscret) {
String access_token = "";
// 獲取AccessToken
String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
+ "&appid=" + appid + "&secret=" + appscret;
HttpMethod method = new GetMethod(token_url);
String res = null;
try {
res = HttpClientUtil.httpRequest(method);
if (!"none".equals(res)) {
JSONObject jsonobj = JSON.parseObject(res);
access_token = jsonobj.getString("access_token");
}
} catch (IOException e) {
return access_token;
} catch (Exception e) {
return access_token;
}
return access_token;
}
通過openId獲取用戶信息接口:
// 根據token,openId獲取用戶信息
public void getUserInfo(String sub_token, String openId){
String UserInfoUrl = "https://api.weixin.qq.com/cgi-bin/user/info" + "?access_token="+ sub_token + "&openid="+ openId + "&lang=zh_CN";
HttpMethod mh = new GetMethod(UserInfoUrl);
String resInfo = null;
try {
resInfo = HttpClientUtil.httpRequest(mh);
if(!"none".equals(resInfo)){
JSONObject j = JSON.parseObject(resInfo);
WxSub wxSub = new WxSub();
wxSub.setId(UUID.randomUUID().toString());
wxSub.setOpenid(openId);
wxSub.setCreatedat(new Date());
String unionId = j.getString("unionid");
String username = j.getString("nickname");
wxSub.setUnionid(unionId);
templateTool.save(wxSub);
System.out.println("unionId:"+unionId);
System.out.println("openId:"+openId);
System.out.println("username:"+username);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}