Java对接钉钉考勤记录的完整步骤
目录
- 一. 基础配置
- 二. 依赖导入及说明
- 三. 获取用户考勤情况
- 每日统计
- 1. 获取部门列表
- 2. 获取部门的子部门列表
- 3. 获取部门用户userid列表
- 4. 根据系统中的用户ID查询用户信息
- 6. 获取打卡结果
- 7. 获取打卡详情
- 考勤统计(可指定时间范围)
- 1. 获取考勤报表列定义(用于在获取考勤报表列值制定获取的数据项)
- 2. 获取考勤报表列值
- 总结
一. 基础配置
首先需要创建一个
应用
进入应用中点击
凭证与基础信息
获取Client ID (原 AppKey)
和Client Secret (原 AppSecret)
修改
权限管理
中 权限范围为全部员工, 不然获取不到数据
二. 依赖导入及说明
<!-- 说明: 下面的两个钉钉依赖目前来说都需要, 因为新版并没有完全覆盖旧版中的功能 (官方说明: 现状:旧版服务端API和新版服务端API开放的产品能力不同,即新版服务端API未包含全部的服务端API的产品能力,请根据实际需求,选择需要的API接入) 资料链接: https://open.dingtalk.com/document/orgapp/differences-between-server-apis-and-new-server-apis?spm=ding_open_doc.document.0.0.200e2b49DAUrx4 --> <!-- 钉钉新版SDK模块 --> <dependency> <groupId>com.aliyun</groupId> <artifactId>dingtalk</artifactId> <version>2.2.33</version> </dependency> <!-- 钉钉旧版SDK模块 --> <!-- 对于老版本依赖包目前来说还没有办法从中央仓库拉取(中央仓库版本过旧), 只能通过jar包手动的安装到本地或者服务器maven仓库中 详情请见: https://open.dingtalk.com/document/isvapp/download-the-server-side-sdk-2 下载链接: https://open-dev.dingtalk.com/download/openSDK/Java?spm=ding_open_doc.document.0.0.2ce83ca70MjetW --> <dependency> <groupId>com.taobao.sdk</groupId> <artifactId>taobao-sdk-java-auto</artifactId> <version>2025http://www.devze.com.07.17</version> </dependency> <!-- 钉钉Stream模块 用于推送和订阅消息 --> <dependency> <groupId>com.dingtalk.open</groupId> <artifactId>app-stream-client</artifactId> <version>1.3.6</version> </dependency>
三. 获取用户考勤情况
下面提供了两种实现方案. 每日统计 和 考勤统计
每日统计: 需要每天上下班的数据
考勤统计: 需要本月的指定员工指定时间范围的考勤统计
注: 对于考勤统计中需要的用户信息可以参考每日统计
对于已有 用户名称只需要获取userId的情况可以尝试通过下方接口获取
0. 搜索用户userId (通过用户姓名获取用户ID)
接口地址:
https://open.dingtalk.com/document/orgapp/address-book-search-user-id
调试地址:
https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.35c53ca7OHAOZj#/?devType=org&api=contact_1.0%23SearchUser
示例: 搜索姓名为
经测试该接口模糊匹配存在问题, 建议通过午后
的员工fullMatchField = 1
精确
每日统计
1. 获取部门列表
接口地址:
https://open.dingtalk.com/document/orgapp/obtain-the-department-list-v2
调试地址:
https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.7408441ceuCNWZ#/?devType=org&api=dingtalk.oapi.v2.department.listsub
2. 获取部门的子部门列表
接口地址:
https://open.dingtalk.com/document/orgapp/obtain-a-sub-department-id-list-v2
调试地址:
https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.7408441ceuCNWZ#/?devType=org&api=dingtalk.oapi.v2.department.listsubid
3. 获取部门用户userid列表
接口地址:
https://open.dingtalk.com/document/orgapp/query-the-list-of-department-userids
调试地址:
https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.7408441ceuCNWZ#/?devType=org&api=dingtalk.oapi.user.listid
4. 根据系统中的用户ID查询用户信息
接口地址:
htwww.devze.comtps://open.dingtalk.com/document/orgapp/query-user-details
调试地址:
https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.35c53ca7OHAOZj#/?devTypandroide=org&api=dingtalk.oapi.v2.user.get
6. 获取打卡结果
接口地址:
https://open.dingtalk.com/document/orgapp/open-attendance-clock-in-data
调试地址:
https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.35c53ca7OHAOZj#/?devType=org&api=dingtalk.oapi.attendance.list
7. 获取打卡详情
接口地址:
https://open.dingtalk.com/document/orgapp/attendance-clock-in-record-is-open
调试地址:
https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.35c53ca7OHAOZj#/?devType=org&api=dingtalk.oapi.attendance.listRecord
考勤统计(可指定时间范围)
1. 获取考勤报表列定义(用于在获取考勤报表列值制定获取的数据项)
接口地址:
https://open.dingtalk.com/document/orgapp/queries-the-enterprise-attendance-report-column
调试地址:
https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.35c53ca7OHAOZj#/?devType=org&api=dingtalk.oapi.attendance.getattcolumns
2. 获取考勤报表列值
接口地址:
https://open.dingtalk.com/document/orgapp/queries-the-column-value-of-the-attendance-report
调试地址:
https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.35c53ca7OHAOZj#/?devType=org&api=dingtalk.oapi.attendance.getcolumnval
测试用例
/** * @author : Cookie * date : 2025/8/8 */ public class DingDingAPITest { private final String appKey = "dingxw1111111kgdb"; private final String appSecret = "2hN-XXSu7BwpLUSRbvGdeNvEMrfKHjy_1111111111111ThLwYqHFLrL"; /** * 通过appKey和appSecret获取Access_token */ @Test public void getAccessTokenTest() { try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken"); OapiGettokenRequest req = new OapiGettokenRequest(); req.setAppkey(appKey); req.setAppsecret(appSecret); req.setHttpMethod("GET"); OapiGettokenResponse rsp = client.execute(req); System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } } /** * 获取部门列表 */ @Test public void getDepartmentListTest() { try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub"); OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest(); OapiV2DepartmentListsubResponse rsp = client.execute(req, "6d5bd176e2fd3e111111149ae4d104"); System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } } /** * 获取部门的子部门列表 */ @Test public void getMessageTest() { try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid"); OapiV2DepartmentListsubidRequest req = new OapiV2DepartmentListsubidRequest(); req.setDeptId(664446160L); OapiV2DepartmentListsubidResponse rsp = client.execute(req, "6d5bd176e2fd3e111111149ae4d104"); System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } } /** * 获取部门用户userid列表 */ @Test public void userIdList() { try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid"); OapiUserListidRequest req = new OapiUserListidRequest(); req.setDeptId(664286346L); OapiUserListidResponse rsp = client.execute(req, "6d5bd176e2fd3e111111149ae4d104"); System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } } /** * 根据系统中的用户ID查询用户信息 */ @Test public void getUserInfoTest() { try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalkjs.com/topapi/v2/user/get"); OapiV2UserGetRequest req = new OapiV2UserGetRequest(); req.setUserid("1912096523433372683"); OapiV2UserGetResponse rsp = client.execute(req, "6d5bd176e2fd3e111111149ae4d104"); System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } } /** * 获取企业考勤报表列 */ @Test public void getAttendanceColumnsTest() { try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getattcolumns"); OapiAttendanceGetattcolumnsRequest req = new OapiAttendanceGetattcolumnsRequest(); OapiAttendanceGetattcolumnsResponse rsp = client.execute(req, "6d5bd176e2fd3e111111149ae4d104"); System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } } /** * 获取考勤报表列定义 */ @Test public void getAttendanceRecordColumnsTest() { try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getattcolumns"); OapiAttendanceGetattcolumnsRequest req = new OapiAttendanceGetattcolumnsRequest(); OapiAttendanceGetattcolumnsResponse rsp = client.execute(req, "6d5bd176e2fd3e111111149ae4d104"); System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } } /** * 指定用户考勤查询 */ @Test public void getAttendanceRecordTest() { try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getcolumnval"); OapiAttendanceGetcolumnvalRequest req = new OapiAttendanceGetcolumnvalRequest(); req.setUserid("022762312345635565"); req.setColumnIdList("281432955,281432956,281432958,281432961"); req.setFromDate(StringUtils.parseDateT编程ime("2025-07-01 08:00:00")); req.setToDate(StringUtils.parseDateTime("2025-07-31 20:00:00")); OapiAttendanceGetcolumnvalResponse rsp = client.execute(req, "6d5bd176e2fd3e111111149ae4d104"); System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } } /** * 获取打卡详情 */ @Test public void getCheckinRecordTest() { try { DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/attendance/listRecord"); OapiAttendanceListRecordRequest req = new OapiAttendanceListRecordRequest(); req.setUserIds(Arrays.asList("4952111165934", "0227623222235565", "0225373333171716")); req.setCheckDateFrom("2025-08-14 08:00:00"); req.setCheckDateTo("2025-08-20 08:00:00"); OapiAttendanceListRecordResponse rsp = client.execute(req, "6d5bd176e2fd3e111111149ae4d104"); System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } } /** * 钉钉Stream模块测试 */ public static void main(String[] args) throws Exception { OpenDingTalkStreamClientBuilder .custom() .credential(new AuthClientCredential("dingxw1111111kgdb", "2hN-XXSu7BwpLUSRbvGdeNvEMrfKHjy_1111111111111ThLwYqHFLrL")) //注册事件监听 .registerAllEventListener(new GenericEventListener() { public EventAckStatus onEvent(GenericOpenDingTalkEvent event) { try { //事件唯一Id String eventId = event.getEventId(); //事件类型 String eventType = event.getEventType(); //事件产生时间 Long bornTime = event.getEventBornTime(); //获取事件体 jsONObject bizData = event.getData(); //处理事件 // process(bizData); System.out.println(bizData); //消费成功 return EventAckStatus.SUCCESS; } catch (Exception e) { //消费失败 return EventAckStatus.LATER; } } }) .build().start(); } }
总结
到此这篇关于Java对接钉钉考勤记录的文章就介绍到这了,更多相关Java对接钉钉考勤记录内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论