HitBTC api POST 请求,C#
HitBTC api POST request, C#
我知道如何执行 GET 请求,但是 POST 不起作用:
public string Order()
{
var client = new RestClient("http://api.hitbtc.com");
var request = new RestRequest("/api/2/order", Method.POST);
request.AddQueryParameter("nonce", GetNonce().ToString());
request.AddQueryParameter("apikey", HapiKey);
// request.AddParameter("clientOrderId", "");
request.AddParameter("symbol", "BCNUSD");
request.AddParameter("side", "sell");
request.AddParameter("quantity", "10");
request.AddParameter("type", "market");
var body = string.Join("&", request.Parameters.Where(x => x.Type == ParameterType.GetOrPost));
string sign = CalculateSignature(client.BuildUri(request).PathAndQuery + body, HapiSecret);
request.AddHeader("X-Signature", sign);
var response = client.Execute(request);
return response.Content;
}
private static long GetNonce()
{
return DateTime.Now.Ticks * 10;
}
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());
}
}
错误:代码:1001,"Authorization required"。
我的失败在哪里? "apikey" 和 "X-Signature" 是否不再适用于 v2?
非常感谢您对我的帮助!
请检查身份验证 documentation。
您需要使用 public 和私钥进行基本身份验证。
RestSharp 示例:
var client = new RestClient("https://api.hitbtc.com")
{
Authenticator = new HttpBasicAuthenticator(<PublicKey>, <SecretKey>)
};
要创建 API 个密钥,您需要访问“设置”页面。
另外,对于您的 API 操作,您需要将 "Place/cancel orders" 权限设置为 true
。
截图详情:
这里还有完整的代码,很适合我:
var client = new RestClient("https://api.hitbtc.com")
{
Authenticator = new HttpBasicAuthenticator(PublicKey, SecretKey)
};
var request = new RestRequest("/api/2/order", Method.POST)
{
RequestFormat = DataFormat.Json
};
request.AddParameter("symbol", "BCNUSD");
request.AddParameter("side", "sell");
request.AddParameter("quantity", "10");
request.AddParameter("type", "market");
request.AddParameter("timeInForce", "IOC");
var response = client.Execute(request);
if (!response.IsSuccessful)
{
var message = $"REQUEST ERROR (Status Code: {response.StatusCode}; Content: {response.Content})";
throw new Exception(message);
}
我知道如何执行 GET 请求,但是 POST 不起作用:
public string Order()
{
var client = new RestClient("http://api.hitbtc.com");
var request = new RestRequest("/api/2/order", Method.POST);
request.AddQueryParameter("nonce", GetNonce().ToString());
request.AddQueryParameter("apikey", HapiKey);
// request.AddParameter("clientOrderId", "");
request.AddParameter("symbol", "BCNUSD");
request.AddParameter("side", "sell");
request.AddParameter("quantity", "10");
request.AddParameter("type", "market");
var body = string.Join("&", request.Parameters.Where(x => x.Type == ParameterType.GetOrPost));
string sign = CalculateSignature(client.BuildUri(request).PathAndQuery + body, HapiSecret);
request.AddHeader("X-Signature", sign);
var response = client.Execute(request);
return response.Content;
}
private static long GetNonce()
{
return DateTime.Now.Ticks * 10;
}
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());
}
}
错误:代码:1001,"Authorization required"。
我的失败在哪里? "apikey" 和 "X-Signature" 是否不再适用于 v2?
非常感谢您对我的帮助!
请检查身份验证 documentation。
您需要使用 public 和私钥进行基本身份验证。
RestSharp 示例:
var client = new RestClient("https://api.hitbtc.com")
{
Authenticator = new HttpBasicAuthenticator(<PublicKey>, <SecretKey>)
};
要创建 API 个密钥,您需要访问“设置”页面。
另外,对于您的 API 操作,您需要将 "Place/cancel orders" 权限设置为 true
。
截图详情:
这里还有完整的代码,很适合我:
var client = new RestClient("https://api.hitbtc.com")
{
Authenticator = new HttpBasicAuthenticator(PublicKey, SecretKey)
};
var request = new RestRequest("/api/2/order", Method.POST)
{
RequestFormat = DataFormat.Json
};
request.AddParameter("symbol", "BCNUSD");
request.AddParameter("side", "sell");
request.AddParameter("quantity", "10");
request.AddParameter("type", "market");
request.AddParameter("timeInForce", "IOC");
var response = client.Execute(request);
if (!response.IsSuccessful)
{
var message = $"REQUEST ERROR (Status Code: {response.StatusCode}; Content: {response.Content})";
throw new Exception(message);
}