c# - HitBTC api POST 请求,C#

标签 c# post webrequest

我知道如何执行 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/

相关文章:

.net - 使用 webrequest 时如何保持连接?

c# - EF 连接字符串错误

c# - C#中的日期比较

c# - 如何以编程方式登录雅虎网站

python - Google App Engine 中的 cgi.test()

c# - 使用 WebRequests 下载 pdf 文件

c# - 用户控件的结构体属性的代码生成

c# - EF核心: No backing field could be found for property of entity type and the property does not have a getter

java - 如何确保通过 POST 请求从我的应用程序对用户进行身份验证?

google-chrome - 使用 Google Chrome webRequest API 进行简单转发