我想为演示-api.hitbtc.com创建一个机器人。所有的请求都很好。
using System;
using System.Security.Cryptography;
using RestSharp;
using System.Linq;
using System.Text;
static void Main(string[] args)
{
const string apiKey = "xxx";
const string secretKey = "xxx";
var client1 = new RestClient("http://demo-api.hitbtc.com");
var request1 = new RestRequest("/api/1/trading/new_order", Method.POST);
request1.AddParameter("nonce", GetNonce().ToString());
request1.AddParameter("apikey", apiKey);
string sign1 = CalculateSignature(client1.BuildUri(request1).PathAndQuery, secretKey);
request1.AddHeader("X-Signature", sign1);
request1.RequestFormat = DataFormat.Json;
request1.AddBody(new
{
clientOrderId = "58f32654723a4b60ad6b",
symbol = "BTCUSD",
side = "buy",
quantity = "0.01",
type = "market",
timeInForce = "GTC"
});
var response1 = client1.Execute(request1);
Console.WriteLine(response1.Content);
Console.ReadLine();
}
private static long GetNonce()
{
return DateTime.Now.Ticks * 10 / TimeSpan.TicksPerMillisecond; // use millisecond timestamp or whatever you want
}
public static string CalculateSignature(string text, string secretKey)
{
using (var hmacsha512 = new HMACSHA512(Encoding.UTF8.GetBytes(secretKey)))
{
hmacsha512.ComputeHash(Encoding.UTF8.GetBytes(text));
return string.Concat(hmacsha512.Hash.Select(b => b.ToString("x2")).ToArray()); // minimalistic hex-encoding and lower case
}
}但是,当我想尝试POST请求时,我得到了以下错误:
{"code":"InvalidContent","message":"Missing apikey parameter"}在hitbtc.com中,API文档曾说过:“每个请求都应该包括以下参数: nonce、apikey、signature”。问题出在哪里?
发布于 2017-06-06 15:14:08
在执行POST操作时,RestSharp默认会删除查询字符串参数。要解决这个问题,您需要告诉它您的参数是查询字符串参数:
request1.AddQueryParameter("nonce", GetNonce().ToString());
request1.AddQueryParameter("apikey", apiKey);而不是使用reqest1.AddParameter(name, value)
https://stackoverflow.com/questions/44393280
复制相似问题