钉钉-获取日志、审批等信息

本文介绍了一种利用钉钉开放API进行日志、审批备份的方法,避免了传统网页操作的繁琐与不稳定,通过Java实现了自动化获取日志信息,并解析JSON格式数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近接了一个任务,做一个钉钉的日志、审批备份。

看了钉钉开发手册,发现钉钉有着类似微信小程序的一整套办公生态圈。

 然后呢,着手做吧。

最开始想做一个微应用,发现微应用是一个H5的应用,类似一个网页,申请还是挺多要求的,然后也发现并不太适合处理后台备份功能。

然后转向python+selenium一套做自动测试的思路去做这些。

selenium是在学习软件测试中用到的组件,无非就是自动测试工具。 

使用chrome的selenium插件录制了模仿登录钉钉管理后台然后下载日志文件的操作,生成脚本代码。

寻找有效的网页模块,然后编写python脚本,发现还是不理想。

        driver = self.driver
        driver.get("https://oa.dingtalk.com/index.htm#/login")
        xf = self.driver.find_element_by_xpath('//iframe[@ng-if="!isDingLogin && contextStatus==2"]')
        self.driver.switch_to.frame(xf)
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='扫码登录'])[1]/following::div[1]").click()
        driver.find_element_by_id("phone").send_keys("18811452903")
        driver.find_element_by_id("pwd").send_keys("dxf19961118")
        driver.find_element_by_id("loginBtn").click()
        driver.find_element_by_id("orgPwd1").send_keys("DuXiaoFeng1996")
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='管理密码'])[1]/following::div[3]").click()
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='通讯录'])[1]/following::span[1]").click()
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)=''])[2]/following::div[2]").click()
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='日报'])[1]/following::input[1]").click()
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='查 询'])[1]/following::button[1]").click()
        driver.find_element_by_link_text(u"下载").click()
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='设置'])[1]/following::div[2]").click()

 频繁登录后它会跳出验证码,GG。

最后想到的办法是调用钉钉开放API。

钉钉API提供了一系列后台文件的访问接口,便于公司对接自己的系统,做二次开发。

因为是阿里的,毫无疑问选择java,事实也是如此。使用python的top.api发现版本太老了,开发文档中的方法很多已经不适用。

直接贴代码了。

需要AppKey和AppSecret来获取accessToken实现免登陆。

然后通过访问钉钉开放API来实现访问日志信息,很简单。

package dataBackup;

import java.util.concurrent.TimeUnit;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiReportListRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiReportListResponse;
import com.taobao.api.ApiException;

public class dataBackup {
	public static void main(String []args) throws ApiException {
		//获取accessToken实现免登陆
		DefaultDingTalkClient cli = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
		OapiGettokenRequest req = new OapiGettokenRequest();
		req.setAppkey("appkey");
		req.setAppsecret("appsecret");
		req.setHttpMethod("GET");
		OapiGettokenResponse res = cli.execute(req);
		String accessToken = res.getAccessToken();
		//获取当天日志信息
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/report/list");
		OapiReportListRequest request = new OapiReportListRequest();
		request.setTemplateName("日报");
		request.setStartTime(System.currentTimeMillis()-TimeUnit.DAYS.toMillis(10));
		request.setEndTime(System.currentTimeMillis());
		request.setCursor(0L);
		request.setSize(10L);
		OapiReportListResponse response = client.execute(request, accessToken);
		//解码获取的日志json格式文件
		String logInfo = response.getBody();
		System.out.println(logInfo);
	}
}

运行结果如下,是json格式的日志信息,对其解码,提取data_list的内容,在进行解码,提取contents内容, 一直下去到提取出日志文件需要备份的信息,写入excel即可。

{
	"errcode": 0,
	"result": {
		"data_list": [{
			"contents": [{
				"key": "今日完成工作",
				"sort": "0",
				"type": "1",
				"value": "测试"
			}, {
				"key": "未完成工作",
				"sort": "1",
				"type": "1",
				"value": ""
			}, {
				"key": "需协调工作",
				"sort": "2",
				"type": "1",
				"value": ""
			}],
			"create_time": 1551270485000,
			"creator_id": "manager1825",
			"creator_name": "杜晓枫",
			"dept_name": "测试团队2x",
			"images": [],
			"remark": "",
			"report_id": "1692eed1c1712bcc3ffbb66460998d2e",
			"template_name": "日报"
		}, {
			"contents": [{
				"key": "今日完成工作",
				"sort": "0",
				"type": "1",
				"value": "看看书啊?"
			}, {
				"key": "未完成工作",
				"sort": "1",
				"type": "1",
				"value": ""
			}, {
				"key": "需协调工作",
				"sort": "2",
				"type": "1",
				"value": ""
			}],
			"create_time": 1551408602000,
			"creator_id": "manager1825",
			"creator_name": "杜晓枫",
			"dept_name": "测试团队2x",
			"images": [],
			"remark": "",
			"report_id": "16937289c82003df583d145439492f8e",
			"template_name": "日报"
		}],
		"has_more": false,
		"next_cursor": 1187881695,
		"size": 10
	},
	"request_id": "47q0oo4ij8ae"
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值