【问题标题】:HitBTC api POST request, C#HitBTC api POST 请求,C#
【发布时间】:2018-01-20 18:22:44
【问题描述】:

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

我的失败在哪里? v2 的“apikey”和“X-Signature”不再正确了吗?

非常感谢您帮助我!

【问题讨论】:

    标签: c# post webrequest


    【解决方案1】:

    请查看身份验证documentation

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

    RestSharp 示例:

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

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

    对于您的 API 操作,您还需要将“下订单/取消订单”权限设置为 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);
    }
    

    【讨论】:

    • 为什么HitBtc觉得需要传递私钥?几乎所有其他交易所都使用私钥的散列,从而无需通过网络传递它。我知道这是 https 连接,但仍然...
    • 令人震惊的是,加密货币的加密程度如此糟糕。
    • 基本身份验证似乎不再起作用。他们允许您使用第二种类型的授权
    猜你喜欢
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    相关资源
    最近更新 更多