c# - 币安API下单
问题描述
如何下单期货?我收到两个错误:“未发送所需的时间戳参数,为空/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 + "×tamp=" + 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 + "×tamp=" + timestamp;
解决方案
我强烈推荐 GitHub 上的 Binance Postman 集合,以了解如何构建您的请求: Binance Postman 集合
在此之后,我还推荐在这里找到的币安签名示例:币安签名示例
看起来您的签名是在不包括请求的所有参数的情况下生成的。
Binance 支持在正文或 URL 中为发布请求设置参数。就我个人而言,我只使用了 URL 中的所有内容,但签名必须验证所有参数,而您的 queryString 变量正在被转换为签名,但其他数据随后被发送到有效负载中,并且不包含在签名中.
推荐阅读
- c# - 根据一组标准选择要显示的行
- post - Ansible run_once with_items
- php - 抓取 HTML 表单数据并通过 php 发送电子邮件
- jquery - 如何创建一个函数来使用 jQuery 显示文本并且如果 img src 包含“is.png”则不执行?
- python - 来自深度学习的 LSTM 温度预测示例
- postgresql - 在 PostGraphile 中处理列级选择授权的最佳实践
- azure-monitoring - 使用 Prometheus Azure Monitor ConfigMap 进行身份验证
- java - 使用 Java 的 Twitter API 登录过程
- bash - Airflow BashOperator:将参数传递给外部bash脚本
- azure - 验证的开放 API 规范无法上传到 Azure API 管理