我想在加密货币交易所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
请帮忙解决这个问题。 谢谢
我可以在您提供的代码中发现一些问题。
让我们解决这些问题:
signature = JsonConvert.SerializeObject(signature);
无需序列化签名。代码的修订版本:
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);
请同时检查以下事项:
key
) 和密钥 (secretKey
) 正确且具有所需的权限。如有任何疑问,请随时询问!