I try to connect to the Binance Api
Using C#
And RestSharp
lib. Based on documents I should try something like 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
I try many ways and many stack questions but I found solutions for other languages not c#. this i I done based on: this and 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();
}
but I got this error: Signature for this request is not valid. How can I fix it? Thank you.