微信企业号管理通讯录-Java

还没吃饭呢,刚写了下企业号通讯录接口, 企业号通讯录具备完全开放的接口,你的应用可以调用这些接口管理部门、成员和标签。

你的应用也可以使用部门、成员、标签发消息,或更改应用的可见范围

在通讯录管理下,有3个接口,部门、成员、标签管理三个接口,我分别写了3个对应类,管理通讯录API地址:

http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%80%9A%E8%AE%AF%E5%BD%95


通讯录部门管理类 MGroup:


package jsp.weixin.contacts.util;

import jsp.weixin.ParamesAPI.util.ParamesAPI;
import jsp.weixin.ParamesAPI.util.WeixinUtil;

/**
 * 通讯录部门管理类
 * @author Engineer.Jsp
 * @date 2014.10.10*/
public class MGroup {
	
	// 创建部门地址
	public static String CREATE_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=ACCESS_TOKEN";
	// 更新部门地址
	public static String UPDATE_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=ACCESS_TOKEN";
	// 删除部门地址
	public static String DELETE_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=ACCESS_TOKEN&id=ID";
	// 获取部门列表地址
	public static String GETLIST_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN";
	
	/**
	 * 创建部门
	 * @param name 部门名称。长度限制为1~64个字符
	 * @param parentid 父亲部门id。根部门id为1
	 * */
	public static String Create(String name , String parentid){
		String Postjson = "{\"name\": %s,\"parentid\": %s}";
		return String.format(Postjson, name,parentid);
	}
	
	/**
	 * 更新部门
	 * @param name 更新的部门名称。长度限制为0~64个字符。修改部门名称时指定该参数
	 * @param id 部门id
	 * */
	public static String Update(String name , String id){
		String Postjson = "{\"id\": %s,\"name\": %s}";
		return String.format(Postjson, name,id);
	}
	
	/**
	 * 删除部门
	 * @param id 部门id
	 * */
	public static String Delete(String id){
		String delete_url = DELETE_URL.replace("ID", id);
		return delete_url;
	}
	//示例
	public static void main(String[] args) {
		/**
		 * 创建部门示例
		 * */
		// 调取凭证
		String access_token = WeixinUtil.getAccessToken(ParamesAPI.corpId, ParamesAPI.secret).getToken();
		// 拼装数据
		String PostData = Create("新建部门", "2");
		// 提交数据,获取结果
		int result = WeixinUtil.PostMessage(access_token, "POST", CREATE_URL, PostData);
		// 打印结果
		if(0==result){
			System.out.println("操作成功");
		}
		else {
			System.out.println("操作失败");
		}
	}
}


通讯录成员管理类 MPerson:


package jsp.weixin.contacts.util;

import jsp.weixin.ParamesAPI.util.ParamesAPI;
import jsp.weixin.ParamesAPI.util.WeixinUtil;

/**
 * 通讯录成员管理类
 * @author Engineer.Jsp
 * @date 2014.10.10*/
public class MPerson {
	//创建成员地址
	public static String CREATE_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=ACCESS_TOKEN";
	//更新成员地址
	public static String UPDATA_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=ACCESS_TOKEN";
	//删滁成员地址
	public static String DELETE_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=ACCESS_TOKEN&userid=ID";
	//获取成员地址
	public static String GET_PERSON_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=ID";
	//获取部门成员地址
	public static String GET_GROUP_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=ID&fetch_child=0&status=0";
	
	/**
	 * 创建成员
	 * @param userid 员工UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字符
	 * @param name 成员名称。长度为1~64个字符
	 * @param department 成员所属部门id列表 格式: "department": [x, y]
	 * @param position 职位信息
	 * @param mobile 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
	 * @param gender 性别。gender=0表示男,=1表示女。默认gender=0
	 * @param tel 办公电话。长度为0~64个字符
	 * @param email 邮箱。长度为0~64个字符。企业内必须唯一
	 * @param weixinid 微信号。企业内必须唯一
	 * */
	public static String Create(String  userid,String name ,String position ,String mobile ,String gender,String tel ,String email,String weixinid){
		String PostData = "{\"userid\": %s,\"name\": %s,\"department\": [1, 2],\"position\": %s,\"mobile\": %s,\"gender\": %s,\"tel\": %s,\"email\": %s,\"weixinid\": %s}";
		return String.format(PostData, userid,name,position,mobile,gender,tel,email,weixinid);
	}
	
	/**
	 * 更新成员
	 * @param userid 员工UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字符
	 * @param name 成员名称。长度为1~64个字符
	 * @param department 成员所属部门id列表 格式: "department": [x]
	 * @param position 职位信息
	 * @param mobile 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
	 * @param gender 性别。gender=0表示男,=1表示女。默认gender=0
	 * @param tel 办公电话。长度为0~64个字符
	 * @param email 邮箱。长度为0~64个字符。企业内必须唯一
	 * @param weixinid 微信号。企业内必须唯一
	 * @param enable 启用/禁用成员。1表示启用成员,0表示禁用成员
	 * */
	public static String Updata(String  userid,String name ,String position ,String mobile ,String gender,String tel ,String email,String weixinid,String enable){
		String PostData = "{\"userid\": %s,\"name\": %s,\"department\": [1],\"position\": %s,\"mobile\": %s,\"gender\": %s,\"tel\": %s,\"email\": %s,\"weixinid\": %s,\"enable\": %s}";
		return String.format(PostData, userid,name,position,mobile,gender,tel,email,weixinid,enable);
	}
	
	/**
	 * 删除成员
	 * @param userid 员工UserID。对应管理端的帐号
	 * */
	public static String Delete(String userid){
		String delete_url = DELETE_URL.replace("ID", userid);
		return delete_url;
	}
	/**
	 * 获取成员
	 * @param userid 员工UserID。对应管理端的帐号
	 * */
	public static String GPerson(String userid){
		String getperson_url = GET_PERSON_URL.replace("ID", userid);
		return getperson_url;
	}
	/**
	 * 获取部门成员
	 * @param department_id 获取的部门id
	 * @param fetch_child 1/0:是否递归获取子部门下面的成员 (可选)
	 * @param status 0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加 (可选)
	 * */
	public static String GGroup(String department_id){
		String getgroup_url = GET_GROUP_URL.replace("ID", department_id);
		return getgroup_url;
	}
	//示例
	public static void main(String[] args) {
		/**
		 * 创建成员示例
		 * */
		// 调取凭证
		String access_token = WeixinUtil.getAccessToken(ParamesAPI.corpId, ParamesAPI.secret).getToken();
		// 拼装数据
		String PostData = Create("员工UserID", "Engineer-JSP", "架构师", "150xxxx8524", "0", "0731-80xxx89", "jspping@qq.com", "oYxxxxxxxxxxxxxxx26336o3");
		// 提交数据,获取结果
		int result = WeixinUtil.PostMessage(access_token, "POST", CREATE_URL, PostData);
		// 打印结果
		if(0==result){
			System.out.println("操作成功");
		}
		else {
			System.out.println("操作失败");
		}

	}

}

通讯录标签管理类 MTag:

package jsp.weixin.contacts.util;

import jsp.weixin.ParamesAPI.util.ParamesAPI;
import jsp.weixin.ParamesAPI.util.WeixinUtil;

/**
 * 通讯录标签管理类
 * @author Engineer.Jsp
 * @date 2014.10.10*/
public class MTag {
	//创建标签地址
	public static String CREATE_TAG_URL = "https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=ACCESS_TOKEN";
	//更新标签地址
	public static String UPDATA_TAG_URL = "https://qyapi.weixin.qq.com/cgi-bin/tag/update?access_token=ACCESS_TOKEN";
	//删除标签地址
	public static String DELETE_TAG_URL = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token=ACCESS_TOKEN&tagid=ID";
	//获取标签成员地址
	public static String GET_TAG_PERSON = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token=ACCESS_TOKEN&tagid=ID";
	//增加标签成员地址
	public static String ADD_TAG_PERSON = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers?access_token=ACCESS_TOKEN";
	//删除标签成员地址
	public static String DELETE_TAG_PERSON = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers?access_token=ACCESS_TOKEN";
	
	
	/**
	 * 创建标签
	 * @param tagname 标签名称。长度为1~64个字符,标签不可与其他同组的标签重名,也不可与全局标签重名
	 * */
	public static String Create_Tag(String tagname){
		String PostData = "{\"tagname\": %s}";
		return String.format(PostData, tagname);
	}
	
	/**
	 * 更新标签名字
	 * @param tagid 标签ID
	 * @param tagname 标签名称。最长64个字符
	 * */
	public static String Updata_Tag(String tagid , String tagname){
		String PostData = "{\"tagid\": %s,\"tagname\": %s}";
		return String.format(PostData, tagid,tagname);
	}
	
	/**
	 * 删除标签
	 * @param tagid 标签ID
	 * */
	public static String Delete_Tag(String tagid){
		String delete_url = DELETE_TAG_URL.replace("ID", tagid);
		return delete_url;
	}
	
	/**
	 * 获取标签成员
	 * @param tagid 标签ID
	 * */
	public static String Get_Tag_Person(String tagid){
		String get_tagperson_url = GET_TAG_PERSON.replace("ID", tagid);
		return get_tagperson_url;
	}
	
	/**
	 * 增加标签成员
	 * @param tagid 标签ID
	 * @param userlist 企业员工ID列表 格式:"userlist":[ "user1","user2"]
	 * */
	public static String Add_Tag_Person(String tagid,String userlist){
		String PostData = "{\"tagid\": %s,\"userlist\":%s}";
		return String.format(PostData, tagid,userlist);
	}
	
	/**
	 * 删除标签成员
	 * @param tagid 标签ID
	 * @param userlist 企业员工ID列表 格式:"userlist":[ "user1","user2"]
	 * */
	public static String Delete_Tag_Person(String tagid,String userlist){
		String PostData = "{\"tagid\": %s,\"userlist\":%s}";
		return String.format(PostData, tagid,userlist);
	}
	//示例
	public static void main(String[] args) {
		/**
		 * 创建标签示例
		 * */
		// 调取凭证
		String access_token = WeixinUtil.getAccessToken(ParamesAPI.corpId, ParamesAPI.secret).getToken();
		// 拼装数据
		String PostData = Create_Tag("新建标签");
		// 提交数据,获取结果
		int result = WeixinUtil.PostMessage(access_token, "POST", CREATE_TAG_URL, PostData);
		// 打印结果
		if(0==result){
			System.out.println("操作成功");
		}
		else {
			System.out.println("操作失败");
		}
	}

}
其中WeixinUtil的getAccessToken()和PostMessage()是通用方法,主要是方便调用,减少代码量,下面看看这2个方法的代码
<span style="font-family: Arial, Helvetica, sans-serif;">getAccessToken()</span>
     /** 
	 * 获取access_token 
	 *  
	 * @param CorpID 企业Id 
	 * @param SECRET 管理组的凭证密钥,每个secret代表了对应用、通讯录、接口的不同权限;不同的管理组拥有不同的secret 
	 * @return 
	 */  
	public static AccessToken getAccessToken(String corpID, String secret) {  
	    AccessToken accessToken = null;  
	  
	    String requestUrl = access_token_url.replace("CorpID", corpID).replace("SECRET", secret);  
	    JSONObject jsonObject = HttpRequest(requestUrl, "GET", null);  
	    // 如果请求成功  
	    if (null != jsonObject) {  
	        try {  
	            accessToken = new AccessToken();  
	            accessToken.setToken(jsonObject.getString("access_token"));  
	            accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
	            System.out.println("获取token成功:"+jsonObject.getString("access_token")+"————"+jsonObject.getInt("expires_in"));
	        } catch (Exception e) {  
	            accessToken = null;  
	            // 获取token失败  
	            String error = String.format("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));  
	            System.out.println(error);
	        }  
	    }  
	    return accessToken;  
	}

 
 

 
<strong>PostMessage():</strong>
       
/**
	 * 数据提交与请求通用方法
	 * @param access_token 凭证
	 * @param RequestMt 请求方式
	 * @param RequestURL 请求地址
	 * @param outstr 提交json数据
	 * */
    public static int PostMessage(String access_token ,String RequestMt , String RequestURL , String outstr){
    	int result = 0;
    	RequestURL = RequestURL.replace("ACCESS_TOKEN", access_token);
    	JSONObject jsonobject = WeixinUtil.HttpRequest(RequestURL, RequestMt, outstr);
    	 if (null != jsonobject) {  
 	        if (0 != jsonobject.getInt("errcode")) {  
 	            result = jsonobject.getInt("errcode");  
 	            String error = String.format("操作失败 errcode:{} errmsg:{}", jsonobject.getInt("errcode"), jsonobject.getString("errmsg"));  
 	            System.out.println(error); 
 	        }  
 	    }
    	return result;
    }


 
HttpRequest():
 

  /** 
     * 发起https请求并获取结果 
     *  
     * @param requestUrl 请求地址 
     * @param requestMethod 请求方式(GET、POST) 
     * @param outputStr 提交的数据 
     * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值) 
     */  
	public static JSONObject HttpRequest(String request , String RequestMethod , String output ){
		@SuppressWarnings("unused")
		JSONObject jsonObject = null;
		StringBuffer buffer = new StringBuffer();
		try {
			//建立连接
			URL url = new URL(request);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setUseCaches(false);
			connection.setRequestMethod(RequestMethod);
			if(output!=null){
				OutputStream out = connection.getOutputStream();
				out.write(output.getBytes("UTF-8"));
				out.close();
			}
			//流处理
			InputStream input = connection.getInputStream();
			InputStreamReader inputReader = new InputStreamReader(input,"UTF-8");
			BufferedReader reader = new BufferedReader(inputReader);
			String line;
			while((line=reader.readLine())!=null){
				buffer.append(line);
			}
			//关闭连接、释放资源
			reader.close();
			inputReader.close();
			input.close();
			input = null;
			connection.disconnect();
			jsonObject = JSONObject.fromObject(buffer.toString());
		} catch (Exception e) {
		}
		return jsonObject;
	} 

 

以上就是管理通讯录接口的主要接口

新建部门:

新建成员:

新建标签:

有疑问请再次留言,看到第一时间回复,谢谢大家观看本博!


                
评论 49
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Engineer-Jsp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值