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

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


创建分组

一个公众账号,最多支持创建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;
    }


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

你的关注是对我最大的鼓励!


源码下载地址:

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

参考:

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值