钉钉api调用
Posted CaoPengCheng&
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了钉钉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_token
public DingApiUntil() {
getAccess_token();
}
//有参构造,初始化access_token
public DingApiUntil(String access_token) {
this.access_token = access_token;
}
//双参参构造,其他应用初始化access_token
public 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_token
token = JSONObject.parseObject(response.getBody()).getString("access_token");
//每次获取都应该刷新access_token
this.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<UserDetail>
* */
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<UserDetail>
* */
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_token
count = Integer.parseInt(JSONObject.parseObject(JSONObject.parseObject(rsp.getBody()).getString("result")).getString("count"));
}
} catch (ApiException e) {
e.printStackTrace();
}
return count;
}
//打印list
public void printList() {
dept_idList.forEach(System.out::println);
}
//刷新Access_token
public 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();
@Test
public void countTest() {
System.out.println("公司人数=" + dingApiUntil.getPersonnelCount());
}
@Test
public void selectMassage() {
UserDetail user = dingApiUntil.getMassageByUserId("manager8145");
if (user == null) {
System.out.println("user is null!");
} else {
SystemPython3.7配合Django2.0来调用钉钉(dingding)在线api实时监测员工考勤打卡情况
一、python开发的服务程序,调用钉钉接口向钉钉群推送信息的软件能申请专利吗?