开发者可以使用接口,对公众平台的分组进行查询、创建、修改操作,也可以使用接口在需要时移动用户到某个分组。
一个公众账号,最多支持创建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"}
/**
* 创建群组
* @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"}
/**
* 查询所有分组
* @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"}
/**
* 通过用户的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"}
/**
* 修改分组名
* @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"}
/**
* 移动用户分组
* @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