DataEase V2 API 接口调用指南:如何查询某个组织下的用户列表数据


飞致云 发布于 2024-06-11 / 361 阅读 / 0 评论 /
场景: 当我们想要通过接口查询某个组织下的用户列表时,需要先进行组织切换,然后查询对应组织下的用户列表,涉及的接口包括下面两个: 切换组织接口:/de2api/user/switch/{oId} 查询用户列表接口:/de2api/user/pager/{goPage}/{pageSize} 接口调用

场景:

当我们想要通过接口查询某个组织下的用户列表时,需要先进行组织切换,然后查询对应组织下的用户列表,涉及的接口包括下面两个:

切换组织接口:/de2api/user/switch/{oId}

查询用户列表接口:/de2api/user/pager/{goPage}/{pageSize}

接口调用步骤如下:

1、进入 API Key 页面,创建 API Key,创建后获取到 accessKey 和 secretKey(API Key 是访问 DataEase API 的密钥,具有账户的完全权限)。

2、根据获取到的 accessKey 和 secretKey,生成接口调用需要的 token 认证信息,写入请求头的 x-de-ask-token 参数中进行接口调用,切换组织后,需重新生成 token 认证信息,然后调用获取用户接口获取组织下的用户列表,参考以下代码示例。

package org.example;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

public class Main {

    static String accessKey = "y4z835cnqoioub54";
    static String secretKey = "zvvh6bmfxlftmsbs";

    public static void main(String[] args) {
        try {
            // 切换组织
            switchOrganization("1");

            // 获取用户
            URL url = new URL("https://live.fit2cloud.com/de2api/user/pager/1/10");
            String signature = aesEncrypt(accessKey + "|" + UUID.randomUUID() + "|" + System.currentTimeMillis(), secretKey, accessKey);
            Algorithm algorithm = Algorithm.HMAC256(secretKey);
            JWTCreator.Builder builder = JWT.create();
            builder.withClaim("accessKey", accessKey).withClaim("signature", signature);
            String token = builder.sign(algorithm);

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestProperty("Accept", "application/json;charset=UTF-8");
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestProperty("accessKey", accessKey);
            urlConnection.setRequestProperty("signature", signature);
            urlConnection.setRequestProperty("x-de-ask-token", token);
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);

            // todo: 组织请求体
            String jsonData = "{\n" +
                    "  \"keyword\": \"\",\n" +
                    "  \"conditions\": [\n" +
                    "    {\n" +
                    "      \"field\": \"\",\n" +
                    "      \"operator\": \"\",\n" +
                    "      \"value\": {}\n" +
                    "    }\n" +
                    "  ],\n" +
                    "  \"orders\": []\n" +
                    "}";
            DataOutputStream outputStream = new DataOutputStream(urlConnection.getOutputStream());
            outputStream.write(jsonData.getBytes());
            outputStream.flush();
            urlConnection.connect();

            // 获取响应体
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            StringBuilder responseBody = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                responseBody.append(line);
            }
            System.out.println("响应体:\n" + responseBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 切换组织
     */
    public static void switchOrganization(String orgId) {
        // 接口:/de2api/user/switch/${orgId}
        try {
            URL url = new URL("https://live.fit2cloud.com/de2api/user/switch/" + orgId);
            String signature = aesEncrypt(accessKey + "|" + UUID.randomUUID() + "|" + System.currentTimeMillis(), secretKey, accessKey);
            Algorithm algorithm = Algorithm.HMAC256(secretKey);
            JWTCreator.Builder builder = JWT.create();
            builder.withClaim("accessKey", accessKey).withClaim("signature", signature);
            String token = builder.sign(algorithm);

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestProperty("Accept", "application/json;charset=UTF-8");
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestProperty("accessKey", accessKey);
            urlConnection.setRequestProperty("signature", signature);
            urlConnection.setRequestProperty("x-de-ask-token", token);
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.connect();

            // 获取响应体
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            StringBuilder responseBody = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                responseBody.append(line);
            }
            System.out.println("响应体:\n" + responseBody);
        } catch (Exception exception) {

        }
    }

    public static String aesEncrypt(String src, String secretKey, String iv) throws Exception {
        byte[] raw = secretKey.getBytes("UTF-8");
        SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv1 = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv1);
        byte[] encrypted = cipher.doFinal(src.getBytes("UTF-8"));
        return Base64.encodeBase64String(encrypted);
    }
}

运行上面的示例代码,需要在 pom.xml 中引入以下依赖

<dependency>
    <groupId>com.auth0</groupId>
    <artifactId>java-jwt</artifactId>
    <version>3.12.1</version>
</dependency>

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>



是否对你有帮助?