2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > Java版微信公共号开发之分组管理接口

Java版微信公共号开发之分组管理接口

时间:2023-01-22 18:10:34

相关推荐

Java版微信公共号开发之分组管理接口

开发者可以使用接口,对公众平台的分组进行查询、创建、修改操作,也可以使用接口在需要时移动用户到某个分组。

创建分组

一个公众账号,最多支持创建500个分组。

接口调用请求说明

http请求方式: POST(请使用https协议)https://api./cgi-bin/groups/create?access_token=ACCESS_TOKENPOST数据格式:jsonPOST数据例子:{"group":{"name":"test"}}

参数说明

返回说明正常时的返回JSON数据包示例:

{"group": {"id": 107, "name": "test"}}

参数说明

错误时的JSON数据包示例(该示例为AppID无效错误):

{"errcode":40013,"errmsg":"invalid appid"}

创建分组程序Java实现代码:

/*** 创建群组* @param appId* @param appSecret* @param groupName 群组名称* @return 如{"group": { "id": 107, "name": "test" } }*/public static JSONObject createGroup(String appId, String appSecret, String groupName){String accessToken = getAccessToken(appId, appSecret); //本地方法,获取accessTokenString url = "https://api./cgi-bin/groups/create?access_token=" + accessToken;JSONObject j = new JSONObject();try {j.put("group", new JSONObject().put("name",groupName));} catch (JSONException e) {e.printStackTrace();}String rtn = weixinRequest(url, j.toString(), "POST");//本地方法,根据url提交json内容System.out.println("WeixinManager.createGroup()"+rtn);JSONObject json;try {json = new JSONObject(rtn);} catch (JSONException e) {throw new RuntimeException(e.getMessage(),e);}return json;}

查询所有分组

接口调用请求说明

http请求方式: GET(请使用https协议)https://api./cgi-bin/groups/get?access_token=ACCESS_TOKEN

参数说明

返回说明正常时的返回JSON数据包示例:

{"groups": [{"id": 0, "name": "未分组", "count": 72596}, {"id": 1, "name": "黑名单", "count": 36}, {"id": 2, "name": "星标组", "count": 8}, {"id": 104, "name": "华东媒", "count": 4}, {"id": 106, "name": "★不测试组★", "count": 1}]}

参数说明

错误时的JSON数据包示例(该示例为AppID无效错误):

{"errcode":40013,"errmsg":"invalid appid"}

查询所有分组程序Java实现代码:

/*** 查询所有分组* @param appId* @param appSecret* @return*/public static JSONObject getAllGroups(String appId, String appSecret){String accessToken = getAccessToken(appId, appSecret);String url = "https://api./cgi-bin/groups/get?access_token=" + accessToken;String rtn = weixinRequest(url, null, "GET");System.out.println("WeixinManager.getAllGroups()"+rtn);JSONObject json;try {json = new JSONObject(rtn);} catch (JSONException e) {throw new RuntimeException(e.getMessage(),e);}return json;}

查询用户所在分组

通过用户的OpenID查询其所在的GroupID。

接口调用请求说明

http请求方式: POST(请使用https协议)https://api./cgi-bin/groups/getid?access_token=ACCESS_TOKENPOST数据格式:jsonPOST数据例子:{"openid":"od8XIjsmk6QdVTETa9jLtGWA6KBc"}

参数说明

返回说明正常时的返回JSON数据包示例:

{"groupid": 102}

参数说明

错误时的JSON数据包示例(该示例为OpenID无效错误):

{"errcode":40003,"errmsg":"invalid openid"}

查询用户所在分组程序Java实现代码:

/*** 通过用户的OpenID查询其所在的GroupID* @param appId* @param appSecret* @param openId 用户的OpenID* @return 如:{ "groupid": 102 }*/public static JSONObject getUserGroup(String appId, String appSecret, String openId){String accessToken = getAccessToken(appId, appSecret);String url = "https://api./cgi-bin/groups/getid?access_token=" + accessToken;JSONObject j = new JSONObject();try {j.put("openid", openId);} catch (JSONException e1) {e1.printStackTrace();}String rtn = weixinRequest(url, j.toString(), "POST");System.out.println("WeixinManager.createGroup()"+rtn);JSONObject json;try {json = new JSONObject(rtn);} catch (JSONException e) {throw new RuntimeException(e.getMessage(),e);}return json;}

修改分组名

接口调用请求说明

http请求方式: POST(请使用https协议)https://api./cgi-bin/groups/update?access_token=ACCESS_TOKENPOST数据格式:jsonPOST数据例子:{"group":{"id":108,"name":"test2_modify2"}}

参数说明

返回说明正常时的返回JSON数据包示例:

{"errcode": 0, "errmsg": "ok"}

错误时的JSON数据包示例(该示例为AppID无效错误):

{"errcode":40013,"errmsg":"invalid appid"}

修改分组名程序Java实现代码:

/*** 修改分组名* @param appId* @param appSecret* @param groupId* @param newGroupName* @return 如 {"errcode": 0, "errmsg": "ok"}*/public static JSONObject updateGroup(String appId, String appSecret, String groupId, String newGroupName){String accessToken = getAccessToken(appId, appSecret);String url = "https://api./cgi-bin/groups/update?access_token=" + accessToken;JSONObject j = new JSONObject();JSONObject group = new JSONObject();try {j.put("id", groupId);j.put("name",newGroupName);group.put("group",j);} catch (JSONException e) {e.printStackTrace();}String rtn = weixinRequest(url, group.toString(), "POST");System.out.println("WeixinManager.createGroup()"+rtn);JSONObject json;try {json = new JSONObject(rtn);} catch (JSONException e) {throw new RuntimeException(e.getMessage(),e);}return json;}

移动用户分组

接口调用请求说明

http请求方式: POST(请使用https协议)https://api./cgi-bin/groups/members/update?access_token=ACCESS_TOKENPOST数据格式:jsonPOST数据例子:{"openid":"oDF3iYx0ro3_7jD4HFRDfrjdCM58","to_groupid":108}

参数说明

返回说明正常时的返回JSON数据包示例:

{"errcode": 0, "errmsg": "ok"}

错误时的JSON数据包示例(该示例为AppID无效错误):

{"errcode":40013,"errmsg":"invalid appid"}

移动用户分组程序Java实现代码:

/*** 移动用户分组* @param appId* @param appSecret* @param toGroupId 新分组的id* @param openId 用户id* @return 如 {"errcode": 0, "errmsg": "ok"}*/public static JSONObject updateUserGroup(String appId, String appSecret, String toGroupId, String openId){String accessToken = getAccessToken(appId, appSecret);String url = "https://api./cgi-bin/groups/update?access_token=" + accessToken;JSONObject j = new JSONObject();try {j.put("openid", openId);j.put("to_groupid", toGroupId);} catch (JSONException e) {e.printStackTrace();}String rtn = weixinRequest(url, j.toString(), "POST");System.out.println("WeixinManager.createGroup()"+rtn);JSONObject json;try {json = new JSONObject(rtn);} catch (JSONException e) {throw new RuntimeException(e.getMessage(),e);}return json;}

您的关注是我坚持写作的动力,如果觉得有用,欢迎关注我的微信,海量学习资源免费送!

源码下载地址:

(微信接口工具类,涉及微信分组的获取和管理、图文消息的发送、群组消息的发送,请自行下载相关jar包。)

参考:

http://mp./wiki/index.php?title=%E5%88%86%E7%BB%84%E7%AE%A1%E7%90%86%E6%8E%A5%E5%8F%A3

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。