diff --git a/tcctyn-modules/tcctyn-iot/src/main/java/com/tcctyn/iot/hk/TestApi.java b/tcctyn-modules/tcctyn-iot/src/main/java/com/tcctyn/iot/hk/TestApi.java new file mode 100644 index 0000000..bafc8f1 --- /dev/null +++ b/tcctyn-modules/tcctyn-iot/src/main/java/com/tcctyn/iot/hk/TestApi.java @@ -0,0 +1,91 @@ +package com.tcctyn.iot.hk; + +import com.alibaba.fastjson.JSON; +import com.hikvision.artemis.sdk.ArtemisHttpUtil; +import com.hikvision.artemis.sdk.config.ArtemisConfig; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +/** + * @author Jason + * @date 2025年05月22日 15:13:09 + */ +public class TestApi { + + private static final String ARTEMIS_PATH = "/artemis"; + + /** + * 调用POST请求类型接口,这里以获取组织列表为例 + * 接口实际url:https://ip:port/artemis/api/resource/v1/org/orgList + * @return + */ + public static String callPostApiGetOrgList() throws Exception { + /** + * https://ip:port/artemis/api/resource/v1/org/orgList + * 通过查阅AI Cloud开放平台文档或网关门户的文档可以看到获取组织列表的接口定义,该接口为POST请求的Rest接口, 入参为JSON字符串,接口协议为https。 + * ArtemisHttpUtil工具类提供了doPostStringArtemis调用POST请求的方法,入参可传JSON字符串, 请阅读开发指南了解方法入参,没有的参数可传null + */ + ArtemisConfig config = new ArtemisConfig(); + config.setHost("116.53.205.228:14443"); // 代理API网关nginx服务器ip端口 + config.setAppKey("21887937"); // 秘钥appkey + config.setAppSecret("hwOzOEqxuPDz5frAnEXb");// 秘钥appSecret + + final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/org/orgList"; + Map paramMap = new HashMap();// post请求Form表单参数 + paramMap.put("pageNo", "1"); + paramMap.put("pageSize", "2"); + String body = JSON.toJSON(paramMap).toString(); + Map path = new HashMap(2) { + { + put("https://", getCamsApi); + } + }; + Map headers = new HashMap<>(); + headers.put("Content-Type", "application/json"); + return ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json",headers); + + + } + + public static String calculateSignature(String appKey,String appSecret, String method, String path, + String query, String body, String timestamp) { + try { + // 1. 构建待签名字符串 + String content = method + "\n" + + "application/json\n" + // Accept + "application/json\n" + // Content-Type + "\n" + // Date (可为空) + "x-ca-key:" + appKey + "\n" + + "x-ca-nonce:" + UUID.randomUUID().toString() + "\n" + + "x-ca-timestamp:" + timestamp + "\n" + + path; + + if (query != null && !query.isEmpty()) { + content += "?" + query; + } + + content += "\n" + body; + + // 2. 计算HMAC-SHA256签名 + Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); + SecretKeySpec secret_key = new SecretKeySpec(appSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); + sha256_HMAC.init(secret_key); + byte[] hash = sha256_HMAC.doFinal(content.getBytes(StandardCharsets.UTF_8)); + + return Base64.getEncoder().encodeToString(hash); + } catch (Exception e) { + throw new RuntimeException("计算签名失败", e); + } + } + + public static void main(String[] args) throws Exception { + callPostApiGetOrgList(); + } +}