Yobit api - 无效的标志

时间:2018-02-10 06:34:37

标签: c# api .net-core hmac

我正在尝试向Yobit api提出请求 here。我将此作为回应。

"{\"success\":0,\"error\":\"invalid sign\"}"

我可能在散列参数时犯了一个错误,因为我对此没有多少经验,但它可能是其他的东西。任何帮助将不胜感激。感谢

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);
  }
}

2 个答案:

答案 0 :(得分:1)

您希望以他们使用的相同格式发送散列字符串。 Ty将你的使用陈述改为此。

  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);
  }

答案 1 :(得分:0)

一切正常,没有问题。

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;