钉钉api调用
钉钉api调用
开发须知()官方
(1)调用钉钉服务端接口时,需使用HTTPS协议、JSON数据格式、UTF-8编码,POST请求请在HTTP Header中设置 Content-Type:application/json。
访问域名为:
新版服务端接口:https://api.dingtalk.com。
旧版服务端接口:https://oapi.dingtalk.com。
说明 旧版服务端接口支持正常调用。
(2)在调用服务端接口前,确保你已了解调用频率限制。详情请参考调用频率限制。
(3)在调用服务端接口前,确保你已经设置了对应的接口权限。详情请参考添加接口调用权限。
(4)无论是哪种应用,都必须接入钉钉免登,即在用户打开应用时可直接获取用户身份无需输入钉钉账号和密码。
access_token
access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token。
开发者需要缓存access_token,用于后续接口的调用。因为每个应用的access_token是彼此独立的,所以进行缓存时需要区分应用来进行存储。
不能频繁调用gettoken接口,否则会受到频率拦截。
package DingDingApiDemo;import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;import java.util.*;/*** DingTalkApi工具类** @author CaoPengCheng* @version 1.0.0* @Project DingTalkDemo* @Date 2021-08-31*/
public class DingApiUntil {//应用的唯一标识key。private String appkey = "ding8lrom1le5zopavwt";//应用的密钥。private String appsecret = "T_oZq1IIEj5D5rses0EGc5gyI0EL_jqL8cLGhdFyWcdvpm8eSEwfLNEsJae0ObC-";private DingTalkClient client;private String access_token;private final List<Long> dept_idList = new ArrayList<>();//无参构造,初始化access_tokenpublic DingApiUntil() {getAccess_token();}//有参构造,初始化access_tokenpublic DingApiUntil(String access_token) {this.access_token = access_token;}//双参参构造,其他应用初始化access_tokenpublic DingApiUntil(String appkey, String appsecret) {this.appkey = appkey;this.appsecret = appsecret;}/** 获取access_token* request:get* return:String* */public String getAccess_token() {OapiGettokenResponse response;OapiGettokenRequest request;String token = null;try {client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");request = new OapiGettokenRequest();request.setAppkey(appkey);request.setAppsecret(appsecret);request.setHttpMethod("GET");response = client.execute(request);//将response转为json,取出access_tokentoken = JSONObject.parseObject(response.getBody()).getString("access_token");//每次获取都应该刷新access_tokenthis.access_token = token;} catch (ApiException e) {e.printStackTrace();}return token;}/** 获取所有小部门Id* request:post* return:void* */private void getDept_id(Long deptId) {OapiV2DepartmentListsubidRequest req;OapiV2DepartmentListsubidResponse rsp;try {if (access_token != null) {client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid");req = new OapiV2DepartmentListsubidRequest();req.setDeptId(deptId);rsp = client.execute(req, access_token);String json = JSONObject.parseObject(JSONObject.parseObject(rsp.getBody()).getString("result")).getString("dept_id_list");json = json.replace("]", "");json = json.replace("[", "");if (json.equals("")) {dept_idList.add(Long.valueOf(deptId));} else {for (String str : json.split(",")) {getDept_id(Long.valueOf(str));}}}} catch (ApiException e) {e.printStackTrace();}}/** 根据dept_id获取全部userId* request:post* return:* */public String[] getUserIdAllByDept_id(Long dept_id) {OapiUserListidRequest req;OapiUserListidResponse rsp;String json = null;try {if (access_token != null) {client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");req = new OapiUserListidRequest();req.setDeptId(dept_id);rsp = client.execute(req, access_token);json = JSONObject.parseObject(JSONObject.parseObject(rsp.getBody()).getString("result")).getString("userid_list");json = json.replace("]", "");json = json.replace("[", "");json = json.replace("\"", "");}} catch (ApiException e) {e.printStackTrace();}return json.split(",");}/** 根据userId获取全部信息* request:post* return:UserDetail* */public UserDetail getMassageByUserId(String userId) {OapiV2UserGetRequest req;OapiV2UserGetResponse rsp;UserDetail user = null;try {if (access_token != null) {client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");req = new OapiV2UserGetRequest();req.setUserid(userId);req.setLanguage("zh_CN");rsp = client.execute(req, access_token);JSONObject result = JSONObject.parseObject(JSONObject.parseObject(rsp.getBody()).getString("result"));if (result != null) {user = new UserDetail();user.setCode(result.getString("mobile"));user.setName(result.getString("name"));user.setPhone(result.getString("mobile"));user.setDingUserId(userId);user.setPassWord("123456");}}} catch (ApiException e) {e.printStackTrace();}return user;}/** 通过部门获取所有员工信息* return:List* */ public List<UserDetail> getUserMassgAllByDept(Set<String> oldUserid) {List<UserDetail> userList = null;getDept_id(1L);if (!dept_idList.isEmpty()) {Set<String> setUserId = new TreeSet<>();for (Long deptid : dept_idList) {String[] userid = getUserIdAllByDept_id(deptid);Collections.addAll(setUserId, userid);//自去重}//oldUserid非空去重if (!oldUserid.isEmpty()) {Set<String> UserIdAll = new TreeSet<>();//与系统中的userid去重setFor:for (String id : setUserId) {for (String old : oldUserid) {if (id.equals(old)) {continue setFor;}}UserIdAll.add(id);}setUserId = UserIdAll;}//setUserId非空,取信息if (!setUserId.isEmpty()) {userList = new ArrayList<>();for (String str : setUserId) {userList.add(getMassageByUserId(str));}}}return userList;}/** 电话获取UserId* request:post* return:List* */ public String getUserIdByTelephone(String phone) {OapiV2UserGetbymobileRequest req;OapiV2UserGetbymobileResponse rsp;String userId = null;try {if (access_token != null) {client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");req = new OapiV2UserGetbymobileRequest();req.setMobile(phone);rsp = client.execute(req, access_token);JSONObject result = JSONObject.parseObject(rsp.getBody());if(result.getString("errmsg").equals("ok")){userId=JSONObject.parseObject(result.getString("result")).getString("userid");}}} catch (ApiException e) {e.printStackTrace();}return userId;}/** 电话获取当员工信息* return: UserDetail* */public UserDetail getUserMassgByTelephone(String phone) {String id = getUserIdByTelephone(phone);if (!id.equals("")) {return getMassageByUserId(id);}else {return null;}}/** 获取员工人数* request:post* return:int* */public int getPersonnelCount() {OapiUserCountRequest req;OapiUserCountResponse rsp;int count = 0;try {if (access_token != null) {client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/count");req = new OapiUserCountRequest();req.setOnlyActive(false);rsp = client.execute(req, access_token);//将response转为json,取出access_tokencount = Integer.parseInt(JSONObject.parseObject(JSONObject.parseObject(rsp.getBody()).getString("result")).getString("count"));}} catch (ApiException e) {e.printStackTrace();}return count;}//打印listpublic void printList() {dept_idList.forEach(System.out::println);}//刷新Access_tokenpublic void refreshAccess_token() {this.access_token = getAccess_token();}//TODO:没有授权,获取部到部门信息/** 获取所有部门信息* request:post* return:void* */public void getDepartment() {OapiV2DepartmentListsubRequest req;OapiV2DepartmentListsubResponse rsp;try {if (access_token != null) {client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");req = new OapiV2DepartmentListsubRequest();req.setDeptId(1L);req.setLanguage("zh_CN");rsp = client.execute(req, access_token);System.out.println(rsp.getBody());System.out.println(JSONObject.parseObject(JSONObject.parseObject(rsp.getBody()).getString("result")));}} catch (ApiException e) {e.printStackTrace();}}public void getDepartment2() {try {DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();req.setDeptId(1L);req.setLanguage("zh_CN");OapiV2DepartmentGetResponse rsp;rsp = client.execute(req, access_token);System.out.println(rsp.getBody());} catch (ApiException e) {e.printStackTrace();}}
}
package DingDingApiDemo;import org.junit.Test;import java.util.List;
import java.util.Set;
import java.util.TreeSet;/*** DingTalkApi测试类** @author CaoPengCheng* @version 1.0.0* @project DingTalkDemo* @date 2021-08-31*/
public class DingApiTest {DingApiUntil dingApiUntil = new DingApiUntil();@Testpublic void countTest() {System.out.println("公司人数=" + dingApiUntil.getPersonnelCount());}@Testpublic void selectMassage() {UserDetail user = dingApiUntil.getMassageByUserId("manager8145");if (user == null) {System.out.println("user is null!");} else {System.out.println(user);}}@Testpublic void getUserIdAllByDept_idTest() {dingApiUntil.getUserIdAllByDept_id(537768596L);}@Testpublic void selectUserAll() {Set<String> newId = new TreeSet<>();Set<String> oldId = new TreeSet<>();newId.add("250214042135263180");newId.add("141434602621593606");newId.add("2502254505676061");newId.add("300156295239381872");newId.add("595749044939157569");newId.add("490055366940074226");newId.add("594261420524905377");newId.add("595851032426476806");newId.add("221639481420359250");newId.add("131854291335519272");newId.add("311633010520063562");newId.add("251155622521627291");newId.add("1648535929690701");newId.add("15864876864482337");newId.add("1585648369415541");oldId.add("250214042135263180");oldId.add("141434602621593606");oldId.add("2502254505676061");oldId.add("300156295239381872");oldId.add("595749044939157569");oldId.add("490055366940074226");test(oldId, newId);}public void test(Set<String> oldUserid, Set<String> setUserId) {Set<String> UserIdAll = new TreeSet<>();//与系统中的userid去重setFor:for (String id : setUserId) {for (String old : oldUserid) {if (id.equals(old)) {continue setFor;}}UserIdAll.add(id);}for (String s : UserIdAll)System.out.println(s);}//通过电话精确获取@Testpublic void getUserIdByTelephoneTest() {UserDetail user=dingApiUntil.getUserMassgByTelephone("18993472079");if(user==null)System.out.println("查无此人");elseSystem.out.println(user);}//通过部门全部获取@Testpublic void getuser() {Set<String> oldId = new TreeSet<>();oldId.add("manager8145");oldId.add("01326153421626553659");oldId.add("010937525215978977");oldId.add("manager81450");List<UserDetail> list = dingApiUntil.getUserMassgAllByDept(oldId);if (list == null)System.out.println("error");elselist.forEach(System.out::println);}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
