Group Management Interface for Java public account development
Developers can use interfaces to query, create, and modify groups on the public platform, or use interfaces to move users to a group as needed.
Create Group
A public account can create up to 500 groups.
API call request description
Http Request Method: POST (Please use https protocol) https://api.weixin.qq.com/cgi-bin/groups/create? Access_token = ACCESS_TOKENPOST Data Format: jsonPOST data example: {"group": {"name": "test "}}
Parameter description
Parameters |
Description |
Access_token |
Interface call credential |
Name |
Group Name (up to 30 characters) |
Return descriptionExample of a JSON data packet returned normally:
{ "group": { "id": 107, "name": "test" }}
Parameter description
Parameters |
Description |
Id |
Group id, allocated |
Name |
Group name, UTF-8 encoded |
Example of JSON data packet in case of an error (This example shows an invalid AppID error ):
{"errcode":40013,"errmsg":"invalid appid"}
Java implementation code for creating a grouping program:
/*** Create a group ** @ param appId * @ param appSecret * @ param groupName group name * @ return for example {"group": {"id": 107, "name ": "test"} */public static JSONObject createGroup (String appId, String appSecret, String groupName) {String accessToken = getAccessToken (appId, appSecret); // local method, get 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"); // local method, submit the json content System according to the url. out. println ("WeixinManager. createGroup () "+ rtn); JSONObject json; try {json = new JSONObject (rtn);} catch (JSONException e) {throw new RuntimeException (e. getMessage (), e);} return json ;}
Query all Groups
API call request description
Http Request Method: GET (Please use https protocol) https://api.weixin.qq.com/cgi-bin/groups/get? Access_token = ACCESS_TOKEN
Parameter description
Parameters |
Description |
Access_token |
Interface call credential |
Return descriptionExample of a JSON data packet returned normally:
{"Groups": [{"id": 0, "name": "ungrouped", "count": 72596 },{ "id": 1, "name ": "Blacklist", "count": 36 },{ "id": 2, "name": "Star Group", "count": 8 },{ "id ": 104, "name": "China East Media", "count": 4 },{ "id": 106, "name ":"★Non-Test Group★"," Count ": 1}]}
Parameter description
Parameters |
Description |
Groups |
Public platform group information list |
Id |
Group id, allocated |
Name |
Group name, UTF-8 encoded |
Count |
Number of users in the group |
Example of JSON data packet in case of an error (This example shows an invalid AppID error ):
{"errcode":40013,"errmsg":"invalid appid"}
Query the Java implementation code of all grouping programs:
/*** Query all groups * @ 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 ;}
Query user groups
You can use your OpenID to query the GroupID.
API call request description
Http Request Method: POST (Please use https protocol) https://api.weixin.qq.com/cgi-bin/groups/getid? Access_token = ACCESS_TOKENPOST Data Format: jsonPOST data example: {"openid": "od8XIjsmk6QdVTETa9jLtGWA6KBc "}
Parameter description
Parameters |
Description |
Access_token |
Interface call credential |
Openid |
User's OpenID |
Return descriptionExample of a JSON data packet returned normally:
{ "groupid": 102}
Parameter description
Parameters |
Description |
Groupid |
Groupid to which the user belongs |
Example of JSON data packet in case of an error (this example is an invalid OpenID error ):
{"errcode":40003,"errmsg":"invalid openid"}
Query the Java implementation code of the user's grouping program:
/*** Query the GroupID * @ param appId * @ param appSecret * @ param OpenID * @ return for example: {"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 ;}
Modify group name
API call request description
Http Request Method: POST (Please use https protocol) https://api.weixin.qq.com/cgi-bin/groups/update? Access_token = ACCESS_TOKENPOST Data Format: jsonPOST data example: {"group": {"id": 108, "name": "test2_modify2 "}}
Parameter description
Parameters |
Description |
Access_token |
Interface call credential |
Id |
Group id, allocated |
Name |
Group Name (up to 30 characters) |
Return descriptionExample of a JSON data packet returned normally:
{"errcode": 0, "errmsg": "ok"}
Example of JSON data packet in case of an error (This example shows an invalid AppID error ):
{"errcode":40013,"errmsg":"invalid appid"}
Java implementation code for modifying the group name program:
/*** Modify the group name ** @ param appId * @ param appSecret * @ param groupId * @ param newGroupName * @ return for example {"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 ;}
Mobile User Group
API call request description
Http Request Method: POST (Please use https protocol) https://api.weixin.qq.com/cgi-bin/groups/members/update? Access_token = ACCESS_TOKENPOST Data Format: jsonPOST data example: {"openid": "oDF3iYx0ro3_7jD4HFRDfrjdCM58", "to_groupid": 108}
Parameter description
Parameters |
Description |
Access_token |
Interface call credential |
Openid |
Unique User Identifier |
To_groupid |
Group id |
Return descriptionExample of a JSON data packet returned normally:
{"errcode": 0, "errmsg": "ok"}
Example of JSON data packet in case of an error (This example shows an invalid AppID error ):
{"errcode":40013,"errmsg":"invalid appid"}
Java implementation code of the mobile user grouping program:
/*** Mobile user group ** @ param appId * @ param appSecret * @ param toGroupId id of the new group * @ param openId user id * @ return for example {"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 ;}
Refer:
Http://mp.weixin.qq.com/wiki/index.php? Title = % E5 % 88% 86% E7 % BB % 84% E7 % AE % A1 % E7 % 90% E6 % 8E % A5 % E5 % 8F % A3