- C# 调用 API 查询仪表板列表接口示例代码 {#1.-c%23-%E8%B0%83%E7%94%A8-api-%E6%9F%A5%E8%AF%A2%E4%BB%AA%E8%A1%A8%E6%9D%BF%E5%88%97%E8%A1%A8%E6%8E%A5%E5%8F%A3%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81}
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Jose; // 使用 Jose JWT 库
class Program
{
private static async Task Main()
{
try
{
string accessKey = "75oRTENKkk0JBpFk"; // 请替换为自己的 accessKey
string secretKey = "lGw61osZjZa5SvNA"; // 请替换为自己的 secretKey
string signature = GenerateSignature(accessKey, secretKey);
string token = CreateJwtToken(accessKey, signature, secretKey);
string url = "http://192.168.239.130:9080/de2api/dataVisualization/tree"; // 请替换为自己的 URL
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
httpClient.DefaultRequestHeaders.Add("accessKey", accessKey);
httpClient.DefaultRequestHeaders.Add("signature", signature);
httpClient.DefaultRequestHeaders.Add("x-de-ask-token", token);
string jsonData = "{\"busiFlag\":\"dashboard\"}";
var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("响应体:\n" + responseBody);
}
else
{
Console.WriteLine($"请求失败: {response.StatusCode}");
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private static string GenerateSignature(string accessKey, string secretKey)
{
// 生成 UUID
string uuid = Guid.NewGuid().ToString();
// 获取当前时间戳(毫秒)
long currentTimeMillis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
// 拼接字符串
string input = $"{accessKey}|{uuid}|{currentTimeMillis}";
// 使用 AES 加密生成签名
return AesEncrypt(input, secretKey, accessKey);
}
private static string CreateJwtToken(string accessKey, string signature, string secretKey)
{
var payload = new Dictionary<string, object>
{
{ "accessKey", accessKey },
{ "signature", signature }
};
return JWT.Encode(payload, Encoding.UTF8.GetBytes(secretKey), JwsAlgorithm.HS256);
}
public static string AesEncrypt(string src, string secretKey, string iv)
{
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(secretKey);
aes.IV = Encoding.UTF8.GetBytes(iv);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
byte[] inputBuffer = Encoding.UTF8.GetBytes(src);
cs.Write(inputBuffer, 0, inputBuffer.Length);
cs.FlushFinalBlock();
}
return Convert.ToBase64String(ms.ToArray());
}
}
}
`}`
2.Jose JWT 安装 {#2.jose-jwt-%E5%AE%89%E8%A3%85}
本示例代码使用 Visual Studio Code 工具进行运行,需要去【拓展】下载 NuGet Package Manager 插件;
右键项目添加 NuGet 包;
搜索 jose-jwt 包进行导入项目即可。
- 运行示例代码 {#3.-%E8%BF%90%E8%A1%8C%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81}
成功调用查询仪表板列表 API 接口并返回数据。