MEXC合约API下单请求签名验证失败问题求助
解决MEXC合约下单的Signature verification error问题
你的代码存在几个关键错误导致签名验证失败,逐一修正如下:
1. 错误的签名序列化
你通过JsonConvert.SerializeObject(signature)给签名字符串套了双引号,而MEXC要求签名是纯十六进制字符串,不需要JSON序列化。直接删除这行代码即可。
2. 请求Content设置错误
new StringContent("application/json", Encoding.UTF8)把Content-Type标识当成了请求体内容,完全不符合接口要求。该接口的参数放在URL查询字符串中,POST请求的body应为空,正确写法是:
// 设置空请求体并指定Content-Type request.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
3. 参数未按ASCII升序排序
MEXC的签名规则要求所有请求参数按参数名的ASCII升序排列后再拼接,你当前的参数顺序不符合要求,会导致签名计算错误。修正后的参数顺序应为:openType=1&leverage=3&price=24500&side=1&symbol=BTC_USDT&type=1&vol=0.001
4. 时间同步问题
确保本地时间与MEXC服务器时间误差在5秒内,否则也会触发签名验证失败。建议调用MEXC公共时间接口获取服务器时间替换本地时间:
// 获取服务器时间示例 var timeResponse = await client.GetAsync("https://contract.mexc.com/api/v1/public/time"); var timeJson = await timeResponse.Content.ReadAsStringAsync(); // 解析JSON获取服务器时间戳并替换本地time变量
修正后的完整代码
HttpClient client = new HttpClient(); string key = "你的API_KEY"; string secretKey = "你的SECRET_KEY"; // 参数按ASCII升序排序 string parameter = "openType=1&leverage=3&price=24500&side=1&symbol=BTC_USDT&type=1&vol=0.001"; // 建议使用MEXC服务器时间,避免本地时间偏差 long time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); // 拼接签名源字符串 string param = key + time + parameter; // 计算HMACSHA256签名 using var encryptor = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey)); var resultBytes = encryptor.ComputeHash(Encoding.UTF8.GetBytes(param)); var signature = string.Concat(resultBytes.Select(b => b.ToString("X2"))); // 构建请求 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"https://contract.mexc.com/api/v1/private/order/submit?{parameter}"); // 设置空的请求体,指定Content-Type request.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json"); // 添加请求头 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);
内容的提问来源于stack exchange,提问作者Flax Mine
相关产品推荐
相关产品推荐

