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

时间:2017-06-06 14:51:59

标签: c# restsharp

我想为demo-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.com API Documentation中说:"每个请求都应包含以下参数:nonce,apikey,signature"。 问题在哪里?

1 个答案:

答案 0 :(得分:1)

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

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

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