写关于接口签名的对接
public class Starter
{
private static void Main(string[] args)
{
var client = new RestClient("https://*****");
var resp = client.GetAccessToken("DD", "20250201");
Console.WriteLine(resp);
Console.ReadKey();
}
}
public class SignParam
{
public string nonce { get; set; }
public long timestamp { get; set; }
public string sign { get; set; }
public string keywords { get; set; }
}
public static class ResetClientExtension
{
public static string GetAccessToken(this IRestClient client,string FromType, string keywords)
{
var url = "****";
if (FromType=="DD")
{
url = "/open/v1/kis/business";
}
//获取随机32个字符串
var nonce = Guid.NewGuid().ToString().Replace("-", "");
//获取当前的时间戳
var timestamp = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);
var request = new RestRequest(url, Method.Post);
request.AddHeader("Content-Type", "application/json");
var sb = new StringBuilder();
if (FromType=="KH")
{
if (!string.IsNullOrWhiteSpace(keywords))
sb.Append($"keywords={keywords}&nonce={nonce}");
else
sb.Append($"nonce={nonce}");
}
else if (FromType == "DD")
{
sb.Append($"nonce={nonce}&queryDay={keywords}");
}
sb.Append($"×tamp={timestamp}");
sb.Append("&signKey=E5VehnFxVkUTgTyTBuUB08orlvmduq1F");
//根据属性名称ASCII码排序,空值不参与签名;并且md5加密
var sign = string.Join("", MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(sb.ToString())).Select(x => x.ToString("x2")));
var body = JsonConvert.SerializeObject(new SignParam
{
nonce = nonce,
timestamp = timestamp,
sign = sign,
keywords = keywords
});
request.AddParameter("application/json", body, ParameterType.RequestBody);
var resp = client.Execute(request);
return resp.Content;
}
}