Im trying to access my binance account via the REST API and RestSharp. This is my code:
public void getaccountdata()
{
string apikey = "myapikey";
string secret = "mysecret";
var client = new RestClient("https://api.binance.com");
request = new RestRequest("/api/v3/time", Method.GET);
response = client.Get(request);
ttime testtime = new ttime();
testtime = JsonConvert.DeserializeObject<ttime>response.Content.ToString());
string timestamp = testtime.serverTime;
request = new RestRequest("/api/v3/account", Method.GET);
request.AddHeader("X-MBX-APIKEY", apikey);
request.AddQueryParameter("recvWindow", "5000");
request.AddQueryParameter("timestamp", timestamp);
request.AddQueryParameter("signature", CreateSignature(request.Parameters, secret));
response = client.Get(request);
System.Diagnostics.Debug.WriteLine(response.Content);
}
public static string CreateSignature(List<Parameter> parameters, string secret)
{
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.UTF8.GetBytes(secret);
byte[] queryStringBytes = Encoding.UTF8.GetBytes(signature);
HMACSHA256 hmacsha256 = new HMACSHA256(keyBytes);
byte[] bytes = hmacsha256.ComputeHash(queryStringBytes);
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
}
public class ttime
{
public string serverTime { get; set; }
}
Basically I'm just trying to replicate the solution here. However I always get the "Signature for this request is not valid" response.
The response URI looks correct (timestamp and signature at the end according to the FAQ here)
Does anyone have an idea what I'm doing wrong? thx for any help!