我尝试使用C#和RestSharp库连接Binance Api。根据文档,我应该尝试类似于以下内容:
queryString: symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000×tamp=1499827319559 HMAC SHA256 signature: [linux]$ echo -n "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000×tamp=1499827319559" | openssl dgst -sha256 -hmac "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j" (stdin)= c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71 curl command: (HMAC SHA256) [linux]$ curl -H "X-MBX-APIKEY: vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A" -X POST https://api.binance.com/api/v3/order?symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000×tamp=1499827319559&signature=c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71 我尝试了许多方法并查阅了许多堆栈问题,但我发现其他语言的解决方案并不适用于C#。 这是我基于this和this完成的。
但我收到了这个错误:此请求的签名无效。我该如何修复它?谢谢。
queryString: symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000×tamp=1499827319559 HMAC SHA256 signature: [linux]$ echo -n "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000×tamp=1499827319559" | openssl dgst -sha256 -hmac "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j" (stdin)= c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71 curl command: (HMAC SHA256) [linux]$ curl -H "X-MBX-APIKEY: vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A" -X POST https://api.binance.com/api/v3/order?symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000×tamp=1499827319559&signature=c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71 我尝试了许多方法并查阅了许多堆栈问题,但我发现其他语言的解决方案并不适用于C#。 这是我基于this和this完成的。
public string GetAccountInfo()
{
long timeStamp = GetTimestamp();
var request = new RestRequest("api/v3/account");
request.AddHeader("X-MBX-APIKEY", ApiKey);
request.AddParameter("timestamp", timeStamp, ParameterType.QueryString);
request.AddParameter("recvWindow", RecvWindow, ParameterType.QueryString);
request.AddParameter("signature", GetSignature(request.Parameters), ParameterType.QueryString);
return Client.Get(request).Content;
}
private long GetTimestamp()
{
return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
}
private string GetSignature(List<Parameter> parameters)
{
var signature = "";
if(parameters.Count > 0)
{
foreach (var item in parameters)
{
if(item.Name != "X-MBX-APIKEY")
signature += $"{item.Name}={item.Value}&";
}
signature = signature.Substring(0, signature.Length - 2);
}
byte[] keyBytes = Encoding.Unicode.GetBytes(ApiSecret);
byte[] valueBytes = Encoding.Unicode.GetBytes(signature);
return HashEncode(HashHMAC(keyBytes, valueBytes));
}
private static byte[] HashHMAC(byte[] key, byte[] message)
{
var hash = new HMACSHA256(key);
return hash.ComputeHash(message);
}
private static string HashEncode(byte[] hash)
{
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
但我收到了这个错误:此请求的签名无效。我该如何修复它?谢谢。