Java版微信公共号开发之分组管理接口
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java版微信公共号开发之分组管理接口,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6127字,纯文字阅读大概需要9分钟。
内容图文
开发者可以使用接口,对公众平台的分组进行查询、创建、修改操作,也可以使用接口在需要时移动用户到某个分组。
创建分组
一个公众账号,最多支持创建500个分组。
接口调用请求说明
http请求方式: POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN POST数据格式:json POST数据例子:{"group":{"name":"test"}}
参数说明
参数 | 说明 |
access_token | 调用接口凭证 |
name | 分组名字(30个字符以内) |
返回说明 正常时的返回JSON数据包示例:
{ "group": { "id": 107, "name": "test" } }
参数说明
参数 | 说明 |
id | 分组id,由微信分配 |
name | 分组名字,UTF8编码 |
错误时的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); //本地方法,获取accessToken String url = "https://api.weixin.qq.com/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.weixin.qq.com/cgi-bin/groups/get?access_token=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 } ] }
参数说明
参数 | 说明 |
groups | 公众平台分组信息列表 |
id | 分组id,由微信分配 |
name | 分组名字,UTF8编码 |
count | 分组内用户数量 |
错误时的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.weixin.qq.com/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.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN POST数据格式:json POST数据例子:{"openid":"od8XIjsmk6QdVTETa9jLtGWA6KBc"}
参数说明
参数 | 说明 |
access_token | 调用接口凭证 |
openid | 用户的OpenID |
返回说明 正常时的返回JSON数据包示例:
{ "groupid": 102 }
参数说明
参数 | 说明 |
groupid | 用户所属的groupid |
错误时的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.weixin.qq.com/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.weixin.qq.com/cgi-bin/groups/update?access_token=ACCESS_TOKEN POST数据格式:json POST数据例子:{"group":{"id":108,"name":"test2_modify2"}}
参数说明
参数 | 说明 |
access_token | 调用接口凭证 |
id | 分组id,由微信分配 |
name | 分组名字(30个字符以内) |
返回说明 正常时的返回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.weixin.qq.com/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.weixin.qq.com/cgi-bin/groups/members/update?access_token=ACCESS_TOKEN POST数据格式:json POST数据例子:{"openid":"oDF3iYx0ro3_7jD4HFRDfrjdCM58","to_groupid":108}
参数说明
参数 | 说明 |
access_token | 调用接口凭证 |
openid | 用户唯一标识符 |
to_groupid | 分组id |
返回说明 正常时的返回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.weixin.qq.com/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; }
参考:
http://mp.weixin.qq.com/wiki/index.php?title=%E5%88%86%E7%BB%84%E7%AE%A1%E7%90%86%E6%8E%A5%E5%8F%A3
原文:http://blog.csdn.net/bruce_6/article/details/38873701
内容总结
以上是互联网集市为您收集整理的Java版微信公共号开发之分组管理接口全部内容,希望文章能够帮你解决Java版微信公共号开发之分组管理接口所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。