微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

币安 API 下单

如何解决币安 API 下单

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

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;

解决方法

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

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

您可以使用像 Fiddler 这样的 http 嗅探器来检查您的请求。

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

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

,

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

在此之后,我还推荐此处找到的币安签名示例:Binance Signature Examples

看起来您的签名正在生成,但没有包含请求的所有参数。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?