DataEase v2 使用 C# 调用 API 接口获取数据


飞致云 发布于 2024-12-11 / 29 阅读 / 0 评论 /
1. C# 调用 API 查询仪表板列表接口示例代码 using System; using System.Collections.Generic; using System.Net.Http; using System.Security.Cryptography; using System.Tex

1. C# 调用 API 查询仪表板列表接口示例代码

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 安装

本示例代码使用 Visual Studio Code 工具进行运行,需要去【拓展】下载 NuGet Package Manager 插件;

右键项目添加 NuGet 包;

搜索 jose-jwt 包进行导入项目即可。

3. 运行示例代码

成功调用查询仪表板列表 API 接口并返回数据。



是否对你有帮助?