标签:业务 child 产品经理 stream 开发 信息 port 拼接 调用
获取方式:
登录企业微信—>管理工具—>通讯录同步助手—>开启“API接口同步” ; 开启后,即可看到通讯录密钥,也可设置通讯录API的权限:读取或者编辑通讯录。
获取通讯录密钥的目的:
通过企业ID(CorpId)和 通讯录密钥可以获取通讯录相关接口的使用凭证(AccessToken)。有了AccessToken,就可以使用通讯录相关接口了。
凭证的获取方式有两种(此处暂时存疑,以待勘误):
通讯录AccessToken:CorpId+通讯录密钥
其他AccessToken:CorpId+应用密钥
把对象转换为字节序列的过程称为对象的序列化。
把字节序列恢复为对象的过程称为对象的反序列化。
1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;
2) 在网络上传送对象的字节序列。
(1)java原生序列化
(2)json序列化
而json序列化可使用 ① json-lib
②fastJson
③gson
这里我们采用gson来实现java对象的序列化,需要引入 gson-2.7.jar 包。
(1)简单的Jason
{ "userid": "zhangsan", "name": "张三", "english_name": "jackzhang" }
使用gson.toJson(user)即可将user对象顺序转成json字符串,如下
Gson gson = new Gson(); String jsonU1 =gson.toJson(user);
(2)带数组的json
将department设为list<Integer>类型即可
{ "userid": "zhangsan", "name": "张三", "english_name": "jackzhang" "department": [1, 2], }
(3)包含子对象的json
以下代码为文本消息的json字符串,将text属性类型设为Text,Text类中包含content属性。然后使用 gson.toJson(user)
{ "msgtype" : "text", "agentid" : 1, "text" : { "content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。" }, }
关于Json序列化,读者在 Java企业微信开发_05_消息推送之发送消息 这一节会有更深刻的理解,这一周将会继续总结。
此类封装了微信的相关参数,如企业id、应用凭证、通讯录凭证等。封装起来更易维护,实现一处修改多处改变。
1 package com.ray.util; 2 /** 3 * 微信参数 4 * @author shirayner 5 * 6 */ 7 public class WeiXinParamesUtil { 8 //1.微信参数 9 //token 10 public final static String token = "ray"; 11 // encodingAESKey 12 public final static String encodingAESKey = "z2W9lyOAR1XjY8mopEmiSqib0TlBZzCFiCLp6IdS2Iv"; 13 //企业ID 14 public final static String corpId = "ww92f5da92bb24696e"; 15 16 //应用的凭证密钥 17 public final static String agentSecret = "I73733veH3uCs6H_ijPvIq0skjTaOePsFF4MyCEi3Ag"; 18 //通讯录秘钥 19 public final static String contactsSecret = "1m_9XP62YrXjSiYtL5ThbexiLVWBThukiK5sH7wm1TM"; 20 21 //企业应用的id,整型。可在应用的设置页面查看 22 public final static int agentId = 1000002; 23 24 }
此类封装了http请求、https请求、获取accessToken的方法
1 package com.ray.util; 2 3 import java.io.BufferedReader; 4 import java.io.InputStream; 5 import java.io.InputStreamReader; 6 import java.io.OutputStream; 7 import java.net.ConnectException; 8 import java.net.HttpURLConnection; 9 import java.net.URL; 10 import javax.net.ssl.HttpsURLConnection; 11 import javax.net.ssl.SSLContext; 12 import javax.net.ssl.SSLSocketFactory; 13 import javax.net.ssl.TrustManager; 14 import org.slf4j.Logger; 15 import org.slf4j.LoggerFactory; 16 import com.ray.pojo.AccessToken; 17 import net.sf.json.JSONException; 18 import net.sf.json.JSONObject; 19 20 public class WeiXinUtil { 21 22 private static Logger log = LoggerFactory.getLogger(WeiXinUtil.class); 23 //微信的请求url 24 //获取access_token的接口地址(GET) 限200(次/天) 25 public final static String access_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpId}&corpsecret={corpsecret}"; 26 27 28 29 /** 30 * 1.发起https请求并获取结果 31 * 32 * @param requestUrl 请求地址 33 * @param requestMethod 请求方式(GET、POST) 34 * @param outputStr 提交的数据 35 * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值) 36 */ 37 public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) { 38 JSONObject jsonObject = null; 39 StringBuffer buffer = new StringBuffer(); 40 try { 41 // 创建SSLContext对象,并使用我们指定的信任管理器初始化 42 TrustManager[] tm = { new MyX509TrustManager() }; 43 SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); 44 sslContext.init(null, tm, new java.security.SecureRandom()); 45 // 从上述SSLContext对象中得到SSLSocketFactory对象 46 SSLSocketFactory ssf = sslContext.getSocketFactory(); 47 48 URL url = new URL(requestUrl); 49 HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection(); 50 httpUrlConn.setSSLSocketFactory(ssf); 51 52 httpUrlConn.setDoOutput(true); 53 httpUrlConn.setDoInput(true); 54 httpUrlConn.setUseCaches(false); 55 // 设置请求方式(GET/POST) 56 httpUrlConn.setRequestMethod(requestMethod); 57 58 if ("GET".equalsIgnoreCase(requestMethod)) 59 httpUrlConn.connect(); 60 61 // 当有数据需要提交时 62 if (null != outputStr) { 63 OutputStream outputStream = httpUrlConn.getOutputStream(); 64 // 注意编码格式,防止中文乱码 65 outputStream.write(outputStr.getBytes("UTF-8")); 66 outputStream.close(); 67 } 68 69 // 将返回的输入流转换成字符串 70 InputStream inputStream = httpUrlConn.getInputStream(); 71 InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); 72 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 73 74 String str = null; 75 while ((str = bufferedReader.readLine()) != null) { 76 buffer.append(str); 77 } 78 bufferedReader.close(); 79 inputStreamReader.close(); 80 // 释放资源 81 inputStream.close(); 82 inputStream = null; 83 httpUrlConn.disconnect(); 84 jsonObject = JSONObject.fromObject(buffer.toString()); 85 } catch (ConnectException ce) { 86 log.error("Weixin server connection timed out."); 87 } catch (Exception e) { 88 log.error("https request error:{}", e); 89 } 90 return jsonObject; 91 } 92 93 /** 94 * 2.发起http请求获取返回结果 95 * 96 * @param requestUrl 请求地址 97 * @return 98 */ 99 public static String httpRequest(String requestUrl) { 100 StringBuffer buffer = new StringBuffer(); 101 try { 102 URL url = new URL(requestUrl); 103 HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection(); 104 105 httpUrlConn.setDoOutput(false); 106 httpUrlConn.setDoInput(true); 107 httpUrlConn.setUseCaches(false); 108 109 httpUrlConn.setRequestMethod("GET"); 110 httpUrlConn.connect(); 111 112 // 将返回的输入流转换成字符串 113 InputStream inputStream = httpUrlConn.getInputStream(); 114 InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); 115 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 116 117 String str = null; 118 while ((str = bufferedReader.readLine()) != null) { 119 buffer.append(str); 120 } 121 bufferedReader.close(); 122 inputStreamReader.close(); 123 // 释放资源 124 inputStream.close(); 125 inputStream = null; 126 httpUrlConn.disconnect(); 127 128 } catch (Exception e) { 129 } 130 return buffer.toString(); 131 } 132 133 134 /** 135 * 3.获取access_token 136 * 137 * @param appid 凭证 138 * @param appsecret 密钥 139 * @return 140 */ 141 public static AccessToken getAccessToken(String appid, String appsecret) { 142 AccessToken accessToken = null; 143 144 String requestUrl = access_token_url.replace("{corpId}", appid).replace("{corpsecret}", appsecret); 145 JSONObject jsonObject = httpRequest(requestUrl, "GET", null); 146 // 如果请求成功 147 if (null != jsonObject) { 148 try { 149 accessToken = new AccessToken(); 150 accessToken.setToken(jsonObject.getString("access_token")); 151 accessToken.setExpiresIn(jsonObject.getInt("expires_in")); 152 } catch (JSONException e) { 153 accessToken = null; 154 // 获取token失败 155 log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 156 } 157 } 158 return accessToken; 159 } 160 161 162 }
1 package com.ray.pojo; 2 3 /** 4 * @desc : 微信通用接口凭证 5 * 6 * @author: shirayner 7 * @date : 2017-8-20 下午9:35:11 8 */ 9 public class AccessToken { 10 // 获取到的凭证 11 private String token; 12 // 凭证有效时间,单位:秒 13 private int expiresIn; 14 15 public String getToken() { 16 return token; 17 } 18 19 public void setToken(String token) { 20 this.token = token; 21 } 22 23 public int getExpiresIn() { 24 return expiresIn; 25 } 26 27 public void setExpiresIn(int expiresIn) { 28 this.expiresIn = expiresIn; 29 } 30 }
注:这两个实体类只添加了User的部分属性,如有需要,读者可根据官方文档自行添加相关属性。
1 package com.ray.pojo; 2 3 public class User { 4 private String userid; 5 private String name; 6 private int department; 7 private String mobile; 8 private String email; 9 private String position; 10 private String gender; 11 12 public User(){}; 13 public User(String userid, String name, int department, String mobile, 14 String email, String position, String gender) { 15 super(); 16 this.userid = userid; 17 this.name = name; 18 this.department = department; 19 this.mobile = mobile; 20 this.email = email; 21 this.position = position; 22 this.gender = gender; 23 } 24 public String getUserid() { 25 return userid; 26 } 27 public void setUserid(String userid) { 28 this.userid = userid; 29 } 30 public String getName() { 31 return name; 32 } 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public int getDepartment() { 38 return department; 39 } 40 public void setDepartment(int department) { 41 this.department = department; 42 } 43 public String getMobile() { 44 return mobile; 45 } 46 public void setMobile(String mobile) { 47 this.mobile = mobile; 48 } 49 public String getEmail() { 50 return email; 51 } 52 public void setEmail(String email) { 53 this.email = email; 54 } 55 public String getPosition() { 56 return position; 57 } 58 public void setPosition(String position) { 59 this.position = position; 60 } 61 public String getGender() { 62 return gender; 63 } 64 public void setGender(String gender) { 65 this.gender = gender; 66 } 67 @Override 68 public String toString() { 69 return "User [userid=" + userid + ", name=" + name + ", department=" 70 + department + ", mobile=" + mobile + ", email=" + email 71 + ", position=" + position + ", gender=" + gender + "]"; 72 } 73 74 public String toJson(){ 75 String str="{\"userid\": %s,\"name\": %s,\"department\": [1],\"mobile\": %s,\"email\": %s,\"gender\": %s}"; 76 77 return String.format(str,this.userid,this.name,this.mobile,this.email,this.gender); 78 } 79 80 81 }
1 package com.ray.pojo; 2 3 public class Department { 4 private int id ; 5 private String name; 6 private int parentid; 7 private int order; 8 9 10 11 public Department(int id, String name, int parentid) { 12 super(); 13 this.id = id; 14 this.name = name; 15 this.parentid = parentid; 16 } 17 public int getId() { 18 return id; 19 } 20 public void setId(int id) { 21 this.id = id; 22 } 23 public String getName() { 24 return name; 25 } 26 public void setName(String name) { 27 this.name = name; 28 } 29 public int getParentid() { 30 return parentid; 31 } 32 public void setParentid(int parentid) { 33 this.parentid = parentid; 34 } 35 public int getOrder() { 36 return order; 37 } 38 public void setOrder(int order) { 39 this.order = order; 40 } 41 42 43 }
1 package com.ray.service; 2 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 7 import net.sf.json.JSONObject; 8 9 import org.slf4j.Logger; 10 import org.slf4j.LoggerFactory; 11 12 import com.google.gson.Gson; 13 import com.ray.pojo.User; 14 import com.ray.test.UserTest; 15 import com.ray.util.WeiXinUtil; 16 17 public class Contacts_UserService { 18 private static Logger log = LoggerFactory.getLogger(UserTest.class); 19 20 private static String createUser_url="https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=ACCESS_TOKEN"; 21 private static String getUser_url="https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID"; 22 private static String updateUser_url="https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=ACCESS_TOKEN"; 23 private static String deleteUser_url="https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=ACCESS_TOKEN&userid=USERID"; 24 private static String batchdeleteUser_url="https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete?access_token=ACCESS_TOKEN"; 25 private static String getDepartmentUser_url="https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=FETCH_CHILD"; 26 private static String getDepartmentUserDetails_url="https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=FETCH_CHILD"; 27 28 29 //1.创建成员 30 public void createUser(String accessToken,User user) { 31 32 //1.获取json字符串:将user对象转换为json字符串 33 Gson gson = new Gson(); 34 String jsonU1 =gson.toJson(user); //使用gson.toJson(user)即可将user对象顺序转成json 35 System.out.println("jsonU1:"+jsonU1); 36 37 38 //2.获取请求的url 39 createUser_url=createUser_url.replace("ACCESS_TOKEN", accessToken); 40 41 //3.调用接口,发送请求,创建成员 42 JSONObject jsonObject = WeiXinUtil.httpRequest(createUser_url, "POST", jsonU1); 43 System.out.println("jsonObject:"+jsonObject.toString()); 44 45 //4.错误消息处理 46 if (null != jsonObject) { 47 if (0 != jsonObject.getInt("errcode")) { 48 log.error("创建成员失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 49 } 50 } 51 } 52 53 //2.获取成员 54 public void getUser(String accessToken,String userId) { 55 56 //1.获取请求的url 57 getUser_url=getUser_url.replace("ACCESS_TOKEN", accessToken) 58 .replace("USERID", userId); 59 60 //2.调用接口,发送请求,获取成员 61 JSONObject jsonObject = WeiXinUtil.httpRequest(getUser_url, "GET", null); 62 System.out.println("jsonObject:"+jsonObject.toString()); 63 64 //3.错误消息处理 65 if (null != jsonObject) { 66 if (0 != jsonObject.getInt("errcode")) { 67 log.error("获取成员失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 68 } 69 } 70 } 71 72 //3.更新成员 73 public void updateUser(String accessToken,User user) { 74 75 //1.获取json字符串:将user对象转换为json字符串 76 Gson gson = new Gson(); 77 String jsonU1 =gson.toJson(user); //使用gson.toJson(user)即可将user对象顺序转成json 78 System.out.println("jsonU1:"+jsonU1); 79 80 81 //2.获取请求的url 82 updateUser_url=updateUser_url.replace("ACCESS_TOKEN", accessToken); 83 84 //3.调用接口,发送请求,创建成员 85 JSONObject jsonObject = WeiXinUtil.httpRequest(updateUser_url, "POST", jsonU1); 86 System.out.println("jsonObject:"+jsonObject.toString()); 87 88 //4.错误消息处理 89 if (null != jsonObject) { 90 if (0 != jsonObject.getInt("errcode")) { 91 log.error("更新成员失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 92 } 93 } 94 } 95 96 //4.删除成员 97 public void deleteUser(String accessToken,String userId) { 98 99 //1.获取请求的url 100 deleteUser_url=deleteUser_url.replace("ACCESS_TOKEN", accessToken) 101 .replace("USERID", userId); 102 103 //2.调用接口,发送请求,删除成员 104 JSONObject jsonObject = WeiXinUtil.httpRequest(deleteUser_url, "GET", null); 105 System.out.println("jsonObject:"+jsonObject.toString()); 106 107 //3.错误消息处理 108 if (null != jsonObject) { 109 if (0 != jsonObject.getInt("errcode")) { 110 log.error("删除成员失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 111 } 112 } 113 } 114 115 //5.批量删除成员 116 public void batchdeleteUser(String accessToken,List<String> userIdList){ 117 //1.获取json字符串:将user对象转换为json字符串 118 Map<String, Object> content = new HashMap<String, Object>(); 119 content.put("useridlist", userIdList); 120 121 Gson gson=new Gson(); 122 String useridlist=gson.toJson(content); 123 System.out.println(useridlist); 124 125 //2.获取请求的url 126 batchdeleteUser_url=batchdeleteUser_url.replace("ACCESS_TOKEN", accessToken); 127 128 //3.调用接口,发送请求,创建成员 129 JSONObject jsonObject = WeiXinUtil.httpRequest(batchdeleteUser_url, "POST", useridlist); 130 System.out.println("jsonObject:"+jsonObject.toString()); 131 132 //4.错误消息处理 133 if (null != jsonObject) { 134 if (0 != jsonObject.getInt("errcode")) { 135 log.error("批量删除成员失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 136 } 137 } 138 } 139 140 141 //6.获取部门成员 142 public void getDepartmentUser(String accessToken,String departmentId,String fetchChild) { 143 144 //1.获取请求的url 145 getDepartmentUser_url=getDepartmentUser_url.replace("ACCESS_TOKEN", accessToken) 146 .replace("DEPARTMENT_ID", departmentId) 147 .replace("FETCH_CHILD", fetchChild); 148 149 //2.调用接口,发送请求,获取部门成员 150 JSONObject jsonObject = WeiXinUtil.httpRequest(getDepartmentUser_url, "GET", null); 151 System.out.println("jsonObject:"+jsonObject.toString()); 152 153 //3.错误消息处理 154 if (null != jsonObject) { 155 if (0 != jsonObject.getInt("errcode")) { 156 log.error("获取部门成员失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 157 } 158 } 159 } 160 161 162 //7.获取部门成员详情 163 public void getDepartmentUserDetails(String accessToken,String departmentId,String fetchChild) { 164 //1.获取请求的url 165 getDepartmentUserDetails_url=getDepartmentUserDetails_url.replace("ACCESS_TOKEN", accessToken) 166 .replace("DEPARTMENT_ID", departmentId) 167 .replace("FETCH_CHILD", fetchChild); 168 169 //2.调用接口,发送请求,获取部门成员 170 JSONObject jsonObject = WeiXinUtil.httpRequest(getDepartmentUserDetails_url, "GET", null); 171 System.out.println("jsonObject:"+jsonObject.toString()); 172 173 //3.错误消息处理 174 if (null != jsonObject) { 175 if (0 != jsonObject.getInt("errcode")) { 176 log.error("获取部门成员详情失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 177 } 178 } 179 } 180 181 }
1 package com.ray.service; 2 3 import net.sf.json.JSONObject; 4 import org.slf4j.Logger; 5 import org.slf4j.LoggerFactory; 6 import com.google.gson.Gson; 7 import com.ray.pojo.Department; 8 import com.ray.test.DepartmentTest; 9 import com.ray.util.WeiXinUtil; 10 11 public class Contacts_DepartmentService { 12 private static Logger log = LoggerFactory.getLogger(DepartmentTest.class); 13 14 private static String createDepartment_url="https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=ACCESS_TOKEN"; 15 private static String updateDepartment_url="https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=ACCESS_TOKEN"; 16 private static String deleteDepartment_url="https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=ACCESS_TOKEN&id=ID"; 17 private static String getDepartmentList_url="https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN&id=ID"; 18 19 20 //1.创建部门 21 public void createDepartment(String accessToken,Department department) { 22 23 //1.获取json字符串:将Department对象转换为json字符串 24 Gson gson = new Gson(); 25 String jsonDepartment =gson.toJson(department); //使用gson.toJson(jsonDepartment)即可将jsonDepartment对象顺序转成json 26 System.out.println("jsonDepartment:"+jsonDepartment); 27 //2.拼接请求的url 28 createDepartment_url=createDepartment_url.replace("ACCESS_TOKEN", accessToken); 29 30 //3.调用接口,发送请求,创建部门 31 JSONObject jsonObject = WeiXinUtil.httpRequest(createDepartment_url, "POST", jsonDepartment); 32 System.out.println("jsonObject:"+jsonObject.toString()); 33 if (null != jsonObject) { 34 if (0 != jsonObject.getInt("errcode")) { 35 36 log.error("创建部门失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 37 } 38 } 39 } 40 41 //2.更新部门 42 public void updateDepartment(String accessToken,Department department) { 43 44 //1.获取json字符串:将Department对象转换为json字符串 45 Gson gson = new Gson(); 46 String jsonDepartment =gson.toJson(department); //使用gson.toJson(jsonDepartment)即可将jsonDepartment对象顺序转成json 47 System.out.println("jsonDepartment:"+jsonDepartment); 48 //2.拼接请求的url 49 updateDepartment_url=updateDepartment_url.replace("ACCESS_TOKEN", accessToken); 50 51 //3.调用接口,发送请求,更新部门 52 JSONObject jsonObject = WeiXinUtil.httpRequest(updateDepartment_url, "POST", jsonDepartment); 53 System.out.println("jsonObject:"+jsonObject.toString()); 54 if (null != jsonObject) { 55 if (0 != jsonObject.getInt("errcode")) { 56 57 log.error("更新部门失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 58 } 59 } 60 } 61 62 63 //3.删除部门 64 public void deleteDepartment(String accessToken,String departmentId) { 65 66 //1.获取请求的url 67 deleteDepartment_url=deleteDepartment_url.replace("ACCESS_TOKEN", accessToken) 68 .replace("ID", departmentId); 69 70 //2.调用接口,发送请求,删除部门 71 JSONObject jsonObject = WeiXinUtil.httpRequest(deleteDepartment_url, "GET", null); 72 System.out.println("jsonObject:"+jsonObject.toString()); 73 74 //3.错误消息处理 75 if (null != jsonObject) { 76 if (0 != jsonObject.getInt("errcode")) { 77 log.error("删除部门失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 78 } 79 } 80 } 81 82 83 84 //4.获取部门列表 85 public void getDepartmentList(String accessToken,String departmentId) { 86 87 //1.获取请求的url 88 getDepartmentList_url=getDepartmentList_url.replace("ACCESS_TOKEN", accessToken) 89 .replace("ID", departmentId); 90 91 //2.调用接口,发送请求,获取成员 92 JSONObject jsonObject = WeiXinUtil.httpRequest(getDepartmentList_url, "GET", null); 93 System.out.println("jsonObject:"+jsonObject.toString()); 94 95 //3.错误消息处理 96 if (null != jsonObject) { 97 if (0 != jsonObject.getInt("errcode")) { 98 log.error("获取部门列表 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); 99 } 100 } 101 } 102 103 104 }
1 package com.ray.test; 2 3 import java.util.Arrays; 4 import java.util.List; 5 6 import org.junit.Test; 7 8 import com.ray.pojo.User; 9 import com.ray.service.Contacts_UserService; 10 import com.ray.util.WeiXinParamesUtil; 11 import com.ray.util.WeiXinUtil; 12 13 /** 14 * 成员管理 15 * @author shirayner 16 * 17 *要注意的地方: 18 *1.开启通讯录同步之后,才能进行通讯录的相关操作 19 *2.对通讯录操作的凭证access_token,由企业id-cropId和通讯录秘钥secret(而不是应用秘钥)获取。 20 *3.创建成员需要开启通讯录的编辑权限。(管理工具-通讯录同步) 21 *4.java对象转json: 用gson.toJson(user)即可将user对象顺序转成json 22 */ 23 24 public class UserTest { 25 26 //1.创建成员 27 @Test 28 public void testCreateUser() { 29 //1.创建user对象 30 //User user = new User("3", "ray", 1,"13886086292","3057046319@qq.com","技术顾问","1"); 31 User user = new User("4", "jack", 1,"13986086293","4057046319@qq.com","咨询顾问","1"); 32 //User user = new User("5", "tom", 1,"14986086294","5057046319@qq.com","产品经理","1"); 33 34 //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url 35 String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret).getToken(); 36 System.out.println("accessToken:"+accessToken); 37 38 //3.创建成员 39 Contacts_UserService cus=new Contacts_UserService(); 40 cus.createUser(accessToken,user); 41 42 } 43 44 45 //2.获取成员 46 @Test 47 public void testGetUser() { 48 //1.获取userId 49 //String userId="ShiRui"; 50 String userId="4"; 51 52 //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url 53 String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret).getToken(); 54 System.out.println("accessToken:"+accessToken); 55 56 //3.获取成员 57 Contacts_UserService cus=new Contacts_UserService(); 58 cus.getUser(userId, accessToken); 59 } 60 61 //3.更新成员 62 @Test 63 public void testUpdateUser() { 64 65 //1.更改user对象信息 66 //User user = new User("3", "ray", 1,"13886086292","3057046319@qq.com","技术顾问","1"); 67 //User user = new User("4", "jack", 1,"13986086293","4057046319@qq.com","咨询顾问","1"); 68 User user = new User("3", "ray", 1,"13889086292","3057946319@qq.com","咨询顾问","1"); 69 70 //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url 71 String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret).getToken(); 72 System.out.println("accessToken:"+accessToken); 73 74 //3.创建成员 75 Contacts_UserService cus=new Contacts_UserService(); 76 cus.updateUser( accessToken,user); 77 78 } 79 80 //4.删除成员 81 @Test 82 public void testDeleteUser() { 83 //1.获取userId 84 //String userId="ShiRui"; 85 String userId="4"; 86 87 //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url 88 String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret).getToken(); 89 System.out.println("accessToken:"+accessToken); 90 91 //3.创建成员 92 Contacts_UserService cus=new Contacts_UserService(); 93 cus.deleteUser(userId, accessToken); 94 } 95 96 97 //5.批量删除成员 98 @Test 99 public void testbatchdeleteUser() { 100 //1.获取userIdList 101 String userId1="3"; 102 String userId2="4"; 103 List<String> userIdList = Arrays.asList(userId1, userId2); //此时将userIdList转json,则结果为:["3","4"],会报错:{"errcode":40063,"errmsg":"some parameters are empty"} 104 105 //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url 106 String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret).getToken(); 107 System.out.println("accessToken:"+accessToken); 108 109 //3.批量删除成员 110 Contacts_UserService cus=new Contacts_UserService(); 111 cus.batchdeleteUser(accessToken,userIdList); 112 } 113 114 115 //6.获取部门成员 116 @Test 117 public void testGetDepartmentUser() { 118 //1.获取部门ID以及是否获取子部门成员 119 String departmentId="1"; 120 String fetchChild="0"; 121 122 //2.获取accessToken:根据企业id和通讯录密钥获取access_token,并拼接请求url 123 String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret).getToken(); 124 System.out.println("accessToken:"+accessToken); 125 126 //3.获取部门成员 127 Contacts_UserService cus=new Contacts_UserService(); 128 cus.getDepartmentUser(accessToken, departmentId, fetchChild); 129 } 130 131 132 //7.获取部门成员详情 133 @Test 134 public void testGetDepartmentUserDetails() { 135 //1.获取部门ID以及是否获取子部门成员 136 String departmentId="1"; 137 String fetchChild="0"; 138 139 //2.获取accessToken:根据企业id和通讯录密钥获取access_token,并拼接请求url 140 String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret).getToken(); 141 System.out.println("accessToken:"+accessToken); 142 143 //3.获取部门成员 144 Contacts_UserService cus=new Contacts_UserService(); 145 cus.getDepartmentUserDetails(accessToken, departmentId, fetchChild); 146 } 147 }
1 package com.ray.test; 2 3 import org.junit.Test; 4 import com.ray.pojo.Department; 5 import com.ray.service.Contacts_DepartmentService; 6 import com.ray.util.WeiXinParamesUtil; 7 import com.ray.util.WeiXinUtil; 8 9 /** 10 * 部门管理 11 * @author shirayner 12 * 13 * 14 */ 15 16 public class DepartmentTest { 17 18 19 //1.创建部门 20 @Test 21 public void testCreateDepartment() { 22 //1.创建Department对象,并将对象转换成json字符串 23 Department department = new Department(3, "HEC武汉分公司", 2); 24 //Department department = new Department(2, "HEC技术中心", 1); 25 26 //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url 27 String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret).getToken(); 28 System.out.println("accessToken:"+accessToken); 29 30 //3.创建部门 31 Contacts_DepartmentService cds= new Contacts_DepartmentService(); 32 cds.createDepartment(accessToken, department); 33 } 34 35 36 //2.更新部门 37 @Test 38 public void testUpdateDepartment() { 39 //1.创建Department对象,并将对象转换成json字符串 40 Department department = new Department(2, "HEC技术服务中心", 1); 41 42 43 //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url 44 String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret).getToken(); 45 System.out.println("accessToken:"+accessToken); 46 47 //3.更新部门 48 Contacts_DepartmentService cds= new Contacts_DepartmentService(); 49 cds.updateDepartment(accessToken, department); 50 } 51 52 //3.删除部门 53 @Test 54 public void testDeleteDepartment() { 55 //1.获取userId 56 String departmentId="3"; 57 58 //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url 59 String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret).getToken(); 60 System.out.println("accessToken:"+accessToken); 61 62 //3.删除部门 63 Contacts_DepartmentService cds= new Contacts_DepartmentService(); 64 cds.deleteDepartment(accessToken, departmentId); 65 } 66 67 68 /** 69 * 4.获取部门列表 70 * 参数 必须 说明 71 * access_token 是 调用接口凭证 72 * id 否 部门id。获取指定部门及其下的子部门。 如果不填,默认获取全量组织架构 73 */ 74 @Test 75 public void testGetDepartmentList() { 76 //1.获取departmentId 77 String departmentId="1"; 78 79 //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url 80 String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret).getToken(); 81 System.out.println("accessToken:"+accessToken); 82 83 //3.调用接口,发送请求,删除部门 84 Contacts_DepartmentService cds= new Contacts_DepartmentService(); 85 cds.getDepartmentList(accessToken, departmentId); 86 } 87 88 89 }
1.微信企业号官方开发文档:https://work.weixin.qq.com/api/doc#10016
标签:业务 child 产品经理 stream 开发 信息 port 拼接 调用
原文地址:http://www.cnblogs.com/shirui/p/7401514.html