RestSharp:演示中的 POST 请求 - api.hitbtc.com 不起作用

RestSharp: POST request in demo-api.hitbtc.com doesn't work

我想为演示创建一个机器人-api.hitbtc.com。所有 GET 请求都很好。

using System;
using System.Security.Cryptography;
using RestSharp;
using System.Linq;
using System.Text;

static void Main(string[] args)
    {
        const string apiKey = "xxx";
        const string secretKey = "xxx";

        var client1 = new RestClient("http://demo-api.hitbtc.com");
        var request1 = new RestRequest("/api/1/trading/new_order", Method.POST);
        request1.AddParameter("nonce", GetNonce().ToString());
        request1.AddParameter("apikey", apiKey);

        string sign1 = CalculateSignature(client1.BuildUri(request1).PathAndQuery, secretKey);
        request1.AddHeader("X-Signature", sign1);

        request1.RequestFormat = DataFormat.Json;
        request1.AddBody(new
        {
            clientOrderId = "58f32654723a4b60ad6b",
            symbol = "BTCUSD",
            side = "buy",
            quantity = "0.01",
            type = "market",
            timeInForce = "GTC"
        });

        var response1 = client1.Execute(request1);

        Console.WriteLine(response1.Content);
        Console.ReadLine();
}

        private static long GetNonce()
    {
        return DateTime.Now.Ticks * 10 / TimeSpan.TicksPerMillisecond; // use millisecond timestamp or whatever you want
    }

    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()); // minimalistic hex-encoding and lower case
        }
    }

但是当我想尝试 POST 请求时,我得到了这个错误:

{"code":"InvalidContent","message":"Missing apikey parameter"}

在hitbtc.comAPI Documentation中说过:"Each request should include the following parameters: nonce, apikey, signature"。 问题出在哪里?

RestSharp 似乎在执行 POST 操作时默认删除查询字符串参数。要解决此问题,您需要告诉它您的参数是查询字符串参数:

request1.AddQueryParameter("nonce", GetNonce().ToString());
request1.AddQueryParameter("apikey", apiKey);

而不是使用 reqest1.AddParameter(name, value)