|
@@ -0,0 +1,128 @@
|
|
|
+package com.test.xiaoshi.test.controller;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author chenyu
|
|
|
+ * @date 2023/10/11
|
|
|
+ */
|
|
|
+public class GetUsersFromDingDing {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取钉钉的 Access Token
|
|
|
+ * 首先,我们需要获取钉钉的 Access Token,用于后续的 API 调用。可以通过发送 HTTP 请求获取 Access Token
|
|
|
+ * 这段代码中,你需要替换 your_corp_id 和 your_corp_secret 分别为你的企业 ID 和企业密钥
|
|
|
+ */
|
|
|
+ public String getAccessToken() throws IOException {
|
|
|
+ String corpId = "ding54828c9e7edff1a924f2f5cc6abecb85"; //企业id
|
|
|
+ String corpSecret = "cb6c8a2b6a85336c809c81acb2e95b0b"; //企业密钥
|
|
|
+ String url = " + corpId + &corpsecret=" + corpSecret;
|
|
|
+
|
|
|
+ //发送 HTTP 请求获取 Access Token
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ int responseCode = connection.getResponseCode();
|
|
|
+
|
|
|
+ if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
+ // 获取响应结果
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
+ String inputLine;
|
|
|
+ StringBuffer response = new StringBuffer();
|
|
|
+ while ((inputLine = in.readLine()) != null) {
|
|
|
+ response.append(inputLine);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+
|
|
|
+ // 解析响应结果,获取 Access Token
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(response.toString());
|
|
|
+ String accessToken = jsonObject.getString("access_token");
|
|
|
+ return accessToken;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // 处理错误情况
|
|
|
+ // ...
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取部门列表
|
|
|
+ * 接下来,我们需要获取钉钉中的部门列表。这可以通过调用钉钉的部门列表 API 来实现。
|
|
|
+ */
|
|
|
+ public void getDepartmentInfo() throws IOException {
|
|
|
+ String accessToken = getAccessToken();
|
|
|
+ String url = "" + accessToken;
|
|
|
+
|
|
|
+ //发送 HTTP 请求获取部门列表
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ int responseCode = connection.getResponseCode();
|
|
|
+
|
|
|
+ if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
+ // 获取部门列表
|
|
|
+ BufferedReader departmentIn = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
+ String inputLine;
|
|
|
+ StringBuffer response = new StringBuffer();
|
|
|
+ while ((inputLine = departmentIn.readLine()) != null) {
|
|
|
+ response.append(inputLine);
|
|
|
+ }
|
|
|
+ departmentIn.close();
|
|
|
+
|
|
|
+ // 解析部门列表数据
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(response.toString());
|
|
|
+ JSONArray departmentArr = jsonObject.getJSONArray("department");
|
|
|
+ // 处理部门列表数据
|
|
|
+ // ...
|
|
|
+ } else {
|
|
|
+ // 处理错误情况
|
|
|
+ // ...
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取人员列表
|
|
|
+ * 接下来,我们需要获取钉钉中的人员列表。这可以通过调用钉钉的人员列表 API 来实现。
|
|
|
+ */
|
|
|
+ public void getUserInfo() throws IOException {
|
|
|
+ String accessToken = getAccessToken();
|
|
|
+ Integer departmentId = 0;
|
|
|
+
|
|
|
+ String userUrl = " + accessToken + &department_id=" + departmentId;
|
|
|
+
|
|
|
+ //发送 HTTP 请求获取人员列表
|
|
|
+ HttpURLConnection userConnection = (HttpURLConnection) new URL(userUrl).openConnection();
|
|
|
+ userConnection.setRequestMethod("GET");
|
|
|
+ int userResponseCode = userConnection.getResponseCode();
|
|
|
+
|
|
|
+ if (userResponseCode == HttpURLConnection.HTTP_OK) {
|
|
|
+ // 获取人员列表
|
|
|
+ BufferedReader userIn = new BufferedReader(new InputStreamReader(userConnection.getInputStream()));
|
|
|
+ String userInputLine;
|
|
|
+ StringBuffer response = new StringBuffer();
|
|
|
+ while ((userInputLine = userIn.readLine()) != null) {
|
|
|
+ response.append(userInputLine);
|
|
|
+ }
|
|
|
+ userIn.close();
|
|
|
+
|
|
|
+ // 解析人员列表数据
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(response.toString());
|
|
|
+ JSONArray userArray = jsonObject.getJSONArray("userlist");
|
|
|
+ // 处理人员列表数据
|
|
|
+ // ...
|
|
|
+ } else {
|
|
|
+ // 处理错误情况
|
|
|
+ // ...
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|