代码之家  ›  专栏  ›  技术社区  ›  kemmel

HitBTC api POST请求,C#

  •  3
  • kemmel  · 技术社区  · 8 年前

    我知道如何执行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签名”是否不再正确?

    非常感谢你帮助我!

    1 回复  |  直到 8 年前
        1
  •  3
  •   Alexander I.    8 年前

    请查看身份验证 documentation .

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

    RestSharp示例:

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

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

    此外,对于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);
    }