问题描述
我知道如何执行GET请求,但是POST无法正常工作:
I know how to do a GET request, but POST does not work:
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,需要授权".
Error: code: 1001, "Authorization required".
我的失败在哪里?对于第二版,"apikey"和"X-Signature"是否不再正确?
Where is my failure? Is "apikey" and "X-Signature" not correct for v2 anymore?
非常感谢您对我的帮助!
Thank you very much for helping me!
推荐答案
请查看身份验证文档
您需要使用通过公钥和私钥进行的基本身份验证.
You need to use Basic authentication using public and private key.
RestSharp的示例:
Example for RestSharp:
var client = new RestClient("https://api.hitbtc.com")
{
Authenticator = new HttpBasicAuthenticator(<PublicKey>, <SecretKey>)
};
要创建API密钥,您需要访问设置"页面.
For creating API keys you need to visit Setting page.
对于API动作,您还需要将下订单/取消订单"权限设置为true
.
Also for your API action you need to set "Place/cancel orders" permission to true
.
有关屏幕截图的详细信息:
Details on the screenshot:
这里还有完整的代码,对我来说很不错:
Also here is full code which works for me well:
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);
}
这篇关于HitBTC api POST请求,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!