企业号所需信息及详细设置看官方文档,如题所示,这里涉及通讯录管理和消息发送,详情如下:
先测试是否可以正常获取ACCESS_TOKEN
//获取token
String corpid = "";
String corpsecret = "";//
String TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
Map params = new HashMap();
params.put("corpid", corpid);
params.put("corpsecret", corpsecret);
String rtn_token = HttpRequestUtils.sendGet(TOKEN_URL, params);
String access_token = SinaWeiboMid2Id.parserJson(rtn_token, "access_token");
System.out.println("get token result "+access_token);
创建部门(跟部门的id为1,后台手动创建的部门会分配默认id,可通过查询部门列表查看)
//创建部门测试
String CREATE_DEPARTMET = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=ACCESS_TOKEN";
CREATE_DEPARTMET = CREATE_DEPARTMET.replace("ACCESS_TOKEN", access_token);
String postJson = "{\"name\":\"%s\",\"parentid\": %s,\"id\": %s}";
String outputStr=String.format(postJson, "大数据营销部",1,3);
System.out.println(outputStr);
rtn_token = HttpRequestUtils.wxPost(CREATE_DEPARTMET,outputStr);
System.out.println("create part return "+rtn_token);
查找部门来了(id如果不传,查询所有的部门)
//查找部门测试
String SEARCH_DEPARTMET = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
params = new HashMap();
params.put("access_token", access_token);
params.put("id", 3);
rtn_token = HttpRequestUtils.sendGet(SEARCH_DEPARTMET, params);
System.out.println("get department result "+rtn_token);
查找部门所有成员
//查找部门成员信息
String SEARCH_DEPARTMET_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
params = new HashMap();
params.put("access_token", access_token);
params.put("department_id", 3);
rtn_token = HttpRequestUtils.sendGet(SEARCH_DEPARTMET_USER, params);
System.out.println("get department user result "+rtn_token);
创建部门成员
//创建成员测试
String CREATE_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=ACCESS_TOKEN";
CREATE_USER = CREATE_USER.replace("ACCESS_TOKEN", access_token);
postJson = "{\"userid\":\"%s\",\"name\": \"%s\",\"mobile\": \"%s\",\"department\":%d,\"gender\":%d}";
outputStr=String.format(postJson, "4_#","开发人员","**",3,1);
rtn_token = HttpRequestUtils.wxPost(CREATE_USER,outputStr);
System.out.println("create user return "+rtn_token);
查询某成员信息
//查询成员测试
String SERACH_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/get";
params = new HashMap();
params.put("access_token", access_token);
params.put("userid", "3_**");
rtn_token = HttpRequestUtils.sendGet(SERACH_USER, params);
System.out.println("get user result "+rtn_token);
一对一或者一对多发送
//向指定成员发消息
String SEND_USER = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";
SEND_USER = SEND_USER.replace("ACCESS_TOKEN", access_token);
postJson = "{\"touser\":\"%s\",\"toparty\": %d,\"msgtype\": \"%s\",\"agentid\":\"%s\",\"textcard\":{\"title\":\"%s\",\"description\":\"%s\",\"url\":\"%s\",\"btntxt\":\"%s\"}}";
outputStr=String.format(postJson, "##",3,"textcard","**","领奖通知","2016年9月26日 恭喜你抽中iPhone 7一台,领奖码:xxxx请于2016年10月10日前联系行政同事领取","www.baidu.com","领取");
System.out.println(outputStr);
rtn_token = HttpRequestUtils.wxPost(SEND_USER,outputStr);
System.out.println("send user return "+rtn_token);
注:代码中涉及的json内容提取,请求方法等可以谷歌或者度娘