墨西哥。未来的API。验证失败

问题描述 投票:0回答:1

我想在加密货币交易所Mexc下订单,但是在发送私人请求时,我收到此错误“签名验证错误”。我遵循形成请求和计算签名的文档和规则。

以下是完整代码

HttpClient client = new HttpClient();

string key = "";
string secretKey = "";

// parameters of request
string parameter = "leverage=3&openType=1&price=24500&side=1&symbol=BTC_USDT&type=1&vol=0.001";

// current time (Unix)
long time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

// concatenate params 
string param = key + time + parameter;

// calc signature
using var encryptor = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey));
var resultBytes = encryptor.ComputeHash(Encoding.UTF8.GetBytes(param));

var signature = string.Empty;
foreach (var t in resultBytes)
    signature += t.ToString("X2");

signature = JsonConvert.SerializeObject(signature);

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"https://contract.mexc.com/api/v1/private/order/submit?{parameter}");

request.Content = new StringContent("application/json", Encoding.UTF8);

// add headers
request.Headers.Add("ApiKey", key);
request.Headers.Add("Request-Time", time.ToString());
request.Headers.Add("Signature", signature);

HttpResponseMessage response = await client.SendAsync(request);

string responseBody = await response.Content.ReadAsStringAsync();

// {\"success\":false,\"code\":602,\"message\":\"Signature verification failed!\"}
Console.WriteLine(responseBody);

计算签名文档中的链接 https://mxcdevelop.github.io/apidocs/contract_v1_en/#authentication-method

从文档到发送订单方法的链接 https://mxcdevelop.github.io/apidocs/contract_v1_en/#order-under-maintenance

请帮忙解决这个问题。 谢谢

c# api cryptography dotnet-httpclient signature
1个回答
0
投票

HTTP 请求中签名验证失败

我可以在您提供的代码中发现一些问题。

让我们解决这些问题:

  • 删除该行:
    signature = JsonConvert.SerializeObject(signature);
    无需序列化签名。
  • 修复您设置请求内容的方式。仅当 API 需要请求正文中包含有效负载时才设置它。

代码的修订版本:

HttpClient client = new HttpClient();

string key = "";
string secretKey = "";

// Parameters of request
string parameter = "leverage=3&openType=1&price=24500&side=1&symbol=BTC_USDT&type=1&vol=0.001";

// Current time (Unix)
long time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

// concatenate params 
string param = key + time + parameter;

// calc signature
using
var encryptor = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey));
var resultBytes = encryptor.ComputeHash(Encoding.UTF8.GetBytes(param));

var signature = string.Empty;
foreach(var t in resultBytes)
signature += t.ToString("X2");

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $ "https://contract.mexc.com/api/v1/private/order/submit?{parameter}");

// If the API expects a JSON payload in the POST request body, provide it here:
// request.Content = new StringContent(yourPayload, Encoding.UTF8, "application/json");

// add headers
request.Headers.Add("ApiKey", key);
request.Headers.Add("Request-Time", time.ToString());
request.Headers.Add("Signature", signature);

HttpResponseMessage response = await client.SendAsync(request);

string responseBody = await response.Content.ReadAsStringAsync();

Console.WriteLine(responseBody);

请同时检查以下事项:

  • 仔细检查文档,确保您创建的签名符合 Mexc Exchange 的预期。
  • 确保您的 API 密钥 (
    key
    ) 和密钥 (
    secretKey
    ) 正确且具有所需的权限。
  • 确保端点和 HTTP 方法(GET、POST 等)正确。

如有任何疑问,请随时询问!

© www.soinside.com 2019 - 2024. All rights reserved.