企业微信接口开发——通讯录管理(创建、删除)

参考文档

开发API:https://work.weixin.qq.com/api/doc/90000/90135/90194

错误码查询:https://open.work.weixin.qq.com/devtool/query

开发步骤

1、先获取token(每一步操作都需要用到token)

获取token需要用corpid和secret

“我的企业”中可以查看企业ID(corpid)

企业微信接口开发——通讯录管理(创建、删除)_第1张图片

管理工具>>通讯录同步,可以查看secret(权限需要设置为可编辑)

企业微信接口开发——通讯录管理(创建、删除)_第2张图片

获取token代码:

public final String getVXToken(){
		String QYWX_CORP_ID = PropertiesUtil.getApplicationProperties().getProperty("QYWX_CORP_ID");
		String QYWX_CORP_SECRET = PropertiesUtil.getApplicationProperties().getProperty("QYWX_CORP_SECRET");
		String QYWX_TOKEN_URL = PropertiesUtil.getApplicationProperties().getProperty("QYWX_TOKEN_URL");
        String getAccessTokenUrl = QYWX_TOKEN_URL+ "?corpid=" + QYWX_CORP_ID + "&corpsecret=" + QYWX_CORP_SECRET;
        try {
            URL realUrl = new URL(getAccessTokenUrl);
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String result = "";
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            org.json.JSONObject jsonObject = new org.json.JSONObject(result);
            String access_token = jsonObject.getString("access_token");
            return access_token;
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
        return null;
	}

2、创建人员

创建实体、实体封装转json,连接创建人员

public class VX_USERS {
	private String userid;
    private String name;
    private int department;
    private String mobile;
    private String email;
    private String position;
    private String gender;
    private String address;
    
    public VX_USERS(){};
    public VX_USERS(String userid, String name, int department, String mobile,
            String email, String position, String gender,String address) {
        super();
        this.userid = userid;
        this.name = name;
        this.department = department;
        this.mobile = mobile;
        this.email = email;
        this.position = position;
        this.gender = gender;
        this.address = address;
    }
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public int getDepartment() {
        return department;
    }
    public void setDepartment(int department) {
        this.department = department;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPosition() {
        return position;
    }
    public void setPosition(String position) {
        this.position = position;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

创建成员

其中Gson取用的是gson-2.7.jar

public final String createVXUser(VX_USERS vxUsers){
		String token=getVXToken();
        String authHost = "https://qyapi.weixin.qq.com/cgi-bin/user/create?";
        String getCreateUrl = authHost+ "access_token=" + token;
        Gson gson = new Gson(); 
        String outputStr =gson.toJson(vxUsers); 
        try {  
            URL url = new URL(getCreateUrl);  
            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();  
            httpUrlConn.setDoOutput(true);  
            httpUrlConn.setDoInput(true);  
            httpUrlConn.setUseCaches(false);  
            httpUrlConn.setRequestMethod("POST");  
            // 当有数据需要提交时  
            if (null != outputStr) {  
                OutputStream outputStream = httpUrlConn.getOutputStream();  
                // 注意编码格式,防止中文乱码  
                outputStream.write(outputStr.getBytes("UTF-8"));  
                outputStream.close();  
            }  
            InputStream inputStream = httpUrlConn.getInputStream();  
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");  
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
            String str = null;  
            String result="";
            while ((str = bufferedReader.readLine()) != null) {  
                result += str;
            }  
            bufferedReader.close();  
            inputStreamReader.close();  
            // 释放资源  
            inputStream.close();  
            inputStream = null; 
            org.json.JSONObject jsonObject22 = new org.json.JSONObject(result);
            String resl = jsonObject22.getString("errmsg");
            return resl;
        } catch (Exception e) {  
        	e.printStackTrace(System.err);
        }  
        return null;  
	}

3、删除人员

public final String deleteVXUser(String userCode){
		String token=getVXToken();
        String authHost = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?";
        String getAccessTokenUrl = authHost+ "access_token=" + token + "&userid=" + userCode;
        try {
            URL realUrl = new URL(getAccessTokenUrl);
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String result = "";
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            org.json.JSONObject jsonObject = new org.json.JSONObject(result);
            String errcode = jsonObject.getString("errcode");
            if("0".equals(errcode)){
            	return "success";
            }else{
            	return "error";
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
        return "error";
	}

 

 

你可能感兴趣的:(Java学习之路)