【问题标题】:Binance API place order币安API下单
【发布时间】:2021-09-12 14:42:56
【问题描述】:

我如何订购期货?我收到两个错误:“未发送所需的时间戳参数,为空/null,或格式不正确。”或“此请求的签名无效。”

public static async void Order()
    {
        string base_uri = "https://fapi.binance.com/fapi/v1/order?";
        string API_Key = "bSQQlu2k5tf0oSUGZsNGptisIxXLux8wb............................";
        string Secret_Key = "gWPKP66geFL0ryijnlU3TTepS61.............................";

        string symbol = "XRPUSDT";
        string side = "BUY";
        string type = "MARKET";
        string timeInForce = "GTC";

        decimal quantity = 20;
        long recvWindow = 5000;
        long timestamp = GetServerTime();

        string queryString = "symbol=" + symbol + "&side=" + side + "type=" + type + "&timeInForce=" + timeInForce;
        string signature = HMACHASH(queryString, Secret_Key);

        var Payload = new Credentials
        {
            Quantity = quantity,
            RecvWindow = recvWindow,
            Timestamp = timestamp,
            Signature = signature
        };

        var stringPayload = JsonConvert.SerializeObject(Payload);
        var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");

        httpContent.Headers.Add("X-MBX-APIKEY", API_Key);

        using (var httpClient = new HttpClient())
        {

            var httpResponse = await httpClient.PostAsync(base_uri + queryString, httpContent);
            if (httpResponse.Content != null)
            {
                var responseContent = await httpResponse.Content.ReadAsStringAsync();
                Console.WriteLine(responseContent);
            }
        }

    }

这就是我获取时间戳的方式

public static long GetServerTime()
    {
        string str = BinanceResponse("https://fapi.binance.com/fapi/v1/time");
        string[] arr = str.Split('\"');
        str = arr[2].Trim(':', '}');
        return long.Parse(str);
    }

凭证类

internal class Credentials
{
    [JsonProperty("quantity")]
    public decimal Quantity { get; set; }
    [JsonProperty("recvWindow")]
    public long RecvWindow { get; set; }
    [JsonProperty("timestamp")]
    public long Timestamp { get; set; }
    [JsonProperty("signature")]
    public string Signature { get; set; }
}

序列化后

stringPayload = "{"quantity":20.0,"recvWindow":5000,"timestamp":1625061703897,"signature":"2794e66d4e5b5b6338782e058747a567db523.........................."}"

如果我这样尝试:

string queryString = "symbol=" + symbol + "&side=" + side + "&type=" + type + 
            "&timeInForce=" + timeInForce + "&quantity=" + quantity + "&recvWindow=" + 
            recvWindow + "&timestamp=" + timestamp;

string signature = HMACHASH(queryString, Secret_Key);
queryString += "&signature=" + signature;

错误:“此请求的签名无效。”

已解决! 谢谢你们!我使用了 Fiddler,发现 type = "MARKET" 不需要 "timeInForce" 参数。所有的问题都是因为他。

string queryString = "symbol=" + symbol + "&side=" + side + "&type=" + type + 
             ̶"̶&̶t̶i̶m̶e̶I̶n̶F̶o̶r̶c̶e̶=̶"̶ ̶+̶ ̶t̶i̶m̶e̶I̶n̶F̶o̶r̶c̶e̶  + "&quantity=" + quantity + "&recvWindow=" + 
            recvWindow + "&timestamp=" + timestamp;

【问题讨论】:

标签: c# api timestamp signature binance


【解决方案1】:

我强烈推荐 GitHub 上的 Binance Postman 集合,以了解如何构建您的请求: Binance Postman Collection

在此之后,我还推荐了币安签名示例:Binance Signature Examples

您的签名似乎正在生成没有包括请求的所有参数。

币安支持在正文或 URL 中为发布请求设置参数。就我个人而言,我只使用了 URL 中的所有内容,但签名必须验证所有参数,而您的 queryString 变量正在被转换为签名,但其他数据随后被发送到有效负载中,并且不包含在签名中.

【讨论】:

  • "..URL 中的参数,我可以看到你没有这样做"。我做到了。我收到错误消息:“此请求的签名无效。”我在 c# 上找不到示例请求。仅限 Python 和 Java。
  • 字符串 queryString = "symbol=" + symbol + "&side=" + side + "type=" + type + "&timeInForce=" + timeInForce;在您的示例中,您没有将时间戳或签名附加到此查询字符串。
  • 我做到了。然后我收到错误消息:“此请求的签名无效。”
  • @ИвановИван 我已经编辑了我的答案,您重新检查时的签名仅涵盖正在发送的部分参数,您的签名需要涵盖所有参数。
【解决方案2】:

响应在错误报告中。 Binance API 要求您发送时间戳。

所以你可能没有发送正确的时间戳或没有正确命名它。

您可以使用 Fiddler 等 http 嗅探器检查您的请求。

API 可能区分大小写,因此序列化后的时间戳不应为“时间戳”。检查一下

编辑:您能否提供用于创建请求的文档?因为官方的币安 API 只要求 POST 参数

【讨论】:

  • 官方币安 API:“queryString: symbol=BTCUSDT&side=BUY&type=LIMIT&timeInForce=GTC requestBody: quantity=1&price=9000&recvWindow=5000&timestamp= 1591702613943”。
猜你喜欢
  • 2021-04-27
  • 2018-12-08
  • 2022-11-30
  • 2023-01-04
  • 2021-07-13
  • 2018-06-07
  • 2021-06-12
  • 2021-06-17
  • 2021-11-12
相关资源
最近更新 更多