Data acquisition process with DingTalk API in Java

Scenes

A background management system is required to access DingTalk API to obtain personnel, attendance, approval and other related data.

achieve

DingTalk Open Platform Application Development Document

https://developers.dingtalk.com/document/app

First, you need to contact the administrator of Dingding, log in to Dingding’s backend, and then follow the documentation instructions of the development platform

First create the app

 

Then fill in the basic information of the application

 

The purpose of letting the administrator create an application is to obtain the following two AppKey and AppSecret

 

After using the administrator to log in to Dingding management platform and creating an application, it is to obtain the above AppKey and AppSecret, and then take these two interfaces to call Dingding, and Dingding will return the token

This token is required to call other interfaces.

Note:

Blog homepage:
https://blog.csdn.net/badao_liumang_qizhi
Follow the public
account Domineering
programmers Get programming-related e-books, tutorial pushes and free downloads.

Then in the permission management, you need to set what permissions the app has

 

By default, there are only login permissions and message notification permissions, and then you can add the required permissions in the add interface permissions

 

After the selection is complete, click OK.

Java development process

Dingding officially provides a unified SDK, which can be used to easily call server-side APIs.

 

After downloading, introduce the server sdk into the project

 

In Dingding Open Platform-Development Tools-Server API Debugging Tool-API Explorer, you can directly copy the above two AppKey and AppSecret and then select the language to call

 

For example, the sample code for calling the interface to obtain the token in Java is

    public String getToken() throws ApiException {
        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());
        return rsp.getAccessToken();
    }

Then after obtaining the token, you can use this token to access other interfaces of Dingding

For example, the interface to get the department list in the department management under the address book management, fill in the parameters as required in the middle, and the relevant code will be generated on the right

 

Sample code

public class Main {
    public static void main(String[] args) {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
   OapiDepartmentListRequest req = new OapiDepartmentListRequest();
   req.setLang("zn_CN");
   req.setFetchChild(false);
   req.setId("1");
   req.setHttpMethod("GET");
   OapiDepartmentListResponse rsp = client.execute(req, "dsfsfdsfgs你获取的token");
   System.out.println(rsp.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
}

The access process of other DingTalk API interfaces is similar.

other

1. Because Dingding has restrictions on the frequency of APIs accessed by the interface, it is recommended to call Dingding related interfaces in timed tasks to obtain and store the data in its own database for query.

2. Because of the card replenishment mechanism, if you want to obtain the attendance data, such as allowing the attendance data within 3 days of replenishing the card, then obtain the data of the first four days and cover it every day

3. If the following prompt appears when calling the interface:

The requested department id is not within the authorization range

 

Contact the Dingding administrator to log in to the background and find the permission management under the application

 

Change the scope of authority here to all employees

4. In addition, DingTalk requires that the ip address to access its interface must be configured in advance, and the server export ip in the development management

 

Dingding only allows servers with ip configured here to call its interface

If you use your own computer to call the development interface, you need to obtain the server ip of the public network, and the browser Baidu Ip to obtain the ip of the agent

 

However, this ip will change frequently. Once the ip of the request interface is not in the whitelist, it is necessary to re-query and then reconfigure the export IP.

Therefore, it is recommended to write the timed tasks, directly deploy the data fetching link on the public network server, and configure the server export ip. This ip can be set multiple, separated by commas.

Guess you like

Origin blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/114059494