gpt4 book ai didi

c# - HitBTC api POST 请求,C#

转载 作者:行者123 更新时间:2023-11-30 23:06:13 26 4
gpt4 key购买 nike

我知道如何执行 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,“需要授权”。

我的失败在哪里? “apikey”和“X-Signature”是否不再适用于 v2?

非常感谢您对我的帮助!

最佳答案

请审核身份验证documentation .

您需要使用使用公钥和私钥的基本身份验证。

RestSharp 示例:

var client = new RestClient("https://api.hitbtc.com")
{
Authenticator = new HttpBasicAuthenticator(<PublicKey>, <SecretKey>)
};

要创建 API key ,您需要访问设置页面。

此外,对于您的 API 操作,您需要将“下订单/取消订单”权限设置为 true

截图详情: enter image description here

这里还有完整的代码,对我来说效果很好:

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

关于c# - HitBTC api POST 请求,C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48359629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com