场景:如果您想调用 DataEase 的接口获取数据或者完成相关的操作,可以参考此文章进行操作。
注:DataEase 开放 API 调用是企业版的功能,如需使用,请申请企业版试用。
接口文档入口
接口调用示例,以查询用户列表接口为例:
1、进入 API Key 页面,创建 API Key,创建后获取到 accessKey 和 secretKey(API Key 是访问 DataEase API 的密钥,具有账户的完全权限)。
2、根据获取到的 accessKey 和 secretKey,生成接口调用需要的 token 认证信息,写入请求头的 x-de-ask-token 参数中进行接口调用,参考以下代码示例。
package io.dataease.api;
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 {
// todo: 替换为上一步获取到的 accessKey 和 secretKey
static String accessKey = "y4z835cnqoioub54";
static String secretKey = "zvvh6bmfxlftmsbs";
public static void main(String[] args) {
try {
// todo: 替换为需要调用的接口地址
URL url = new URL("https://live.fit2cloud.com/de2api/user/pager/1/10");
// 生成认证 token
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 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>`
总结
以上步骤是调用 DataEase API 接口获取用户信息的步骤,当您想通过调用其他 API 接口完成其他操作时,方法也是一样的。
如果您在接口文档中没有找到您想要的接口,也可以通过浏览器的 F12 控制台查看功能对应的接口,然后参考上面步骤进行调用。