Reputation: 13
I'm trying to make a request to the Yobit api documented here. I am getting this as a response.
"{\"success\":0,\"error\":\"invalid sign\"}"
I'm probably making a mistake in hashing the parameters as I don't have much experience with that, but it could be something else. Any help would be greatly appreciated. Thanks
private async Task<T> CallPrivateApi<T>(PrivateApiCall call, IRequest requestData) where T: IResponse
{
if (String.IsNullOrWhiteSpace(apiKey) || String.IsNullOrWhiteSpace(apiSecret))
throw new ArgumentNullException("Api Key and Secret are required for private api calls.");
if (client == null)
client = new HttpClient();
var request = new HttpRequestMessage();
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(String.Format("{0}/", PrivateUrl));
var ts = DateTime.Now.Subtract(new DateTime(2018,1,1));
string nonce = ((int)Math.Round(ts.TotalSeconds * 100)).ToString();
string parameters = String.Format("method={0}&{1}&nonce={2}",call, RequestToString(requestData), nonce);
request.Content = new StringContent(parameters, Encoding.UTF8, "application/x-www-form-urlencoded");
request.Content.Headers.Add("Key", apiKey);
using (var hmac = new HMACSHA512(Convert.FromBase64String(apiSecret)))
{
byte[] paramByte = Encoding.UTF8.GetBytes(parameters);
string sign = Convert.ToString(hmac.ComputeHash(paramByte));
request.Content.Headers.Add("Sign", sign);
}
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string json = Encoding.UTF8.GetString(response.Content.ReadAsByteArrayAsync().Result);
return JsonConvert.DeserializeObject<T>(json);
}
else
{
return default(T);
}
}
Upvotes: 1
Views: 556
Reputation: 53
Everything works without problems.
Dictionary<string, string> m = new Dictionary<string, string>();
m.Add("method", "getInfo");
m.Add("nonce", nonce.ToString());
var content = new FormUrlEncodedContent(m);
request.Content = content;
Upvotes: 0
Reputation: 51
You want to send your hashed string in the same format they use. Ty changing your using statement to this.
using(var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(apiSecret)))
{
byte[] signHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(parameters));
string sign = BitConverter.ToString(signHash).ToLower().Replace("-", string.Empty);
request.Content.Headers.Add("Sign", sign);
}
Upvotes: 1