K
K
Karkes2021-09-10 20:21:15
API
Karkes, 2021-09-10 20:21:15

How to fix an error in api request?

Wrote a request to open a market order for the Huobi website according to the documentation . The request is presented below.

var client = new RestClient("https://api.huobi.pro");
string Method = "POST";
string endpoint = "/v1/order/orders/place";
string base_uri = "api.huobi.pro";
var request = new RestRequest("/v1/order/orders/place");
request.AddHeader("Content-Type","application/json");
request.AddQueryParameter("AccessKeyId", apikey);
request.AddQueryParameter("account-id", "12105426");
request.AddQueryParameter("amount", "10");
request.AddQueryParameter("symbol", "expusdt");
request.AddQueryParameter("type", "buy-market");
request.AddQueryParameter("SignatureMethod", "HmacSHA256");
request.AddQueryParameter("SignatureVersion", "2");
request.AddQueryParameter("Timestamp", UTCtime());
request.AddQueryParameter("Signature", CreateSignature(request.Parameters, secret,Method,endpoint,base_uri));
var response = client.Post(request);
string content = response.Content; 
Console.WriteLine(content);

Here is the signature generation code.
public static string CreateSignature(List<Parameter> parameters, string secret,string Method,string endpoint,string base_uri)
            {
                Encoding ascii = Encoding.ASCII;
                string pre_signed_text = Method + "\n" + base_uri + "\n" + endpoint + "\n"  + string.Join("&", parameters).Replace(":","%3A");
                Console.WriteLine(pre_signed_text);
                HMACSHA256 hmac = new HMACSHA256(ascii.GetBytes(secret));
                String calc_sig = Convert.ToBase64String(hmac.ComputeHash(ascii.GetBytes(pre_signed_text)));
                return calc_sig;
            }

When launching a purchase request, it gives an error
{"status":"error","err-code":"api-signature-not-valid","err-msg":"Signature not valid: Verification failure [校验失败]","data":null}

I can't understand what's wrong.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question