Poloniex API "Invalid command" c++ libcurl

Poloniex API "Invalid command" c++ libcurl

本文关键字:c++ libcurl command Invalid API Poloniex      更新时间:2023-10-16

我试图解决它几个小时,但没有什么比这更好的了......

int main() {
CURL *curl_handle = curl_easy_init();
if(curl_handle) {

string post_data="";
struct curl_slist *headers=NULL;
string command = "command=returnBalances&nonce=" + to_string(time(0));
cout<<command<<endl;
string Secret = "mySecretCode";
string Sign = "Sign: "+ hmac::get_hmac(Secret, command, hmac::TypeHash::SHA512);
cout<<Sign<<endl;
post_data += command;
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
headers = curl_slist_append(headers, "Key: myKey");
headers = curl_slist_append(headers, Sign.c_str());
curl_easy_setopt(curl_handle, CURLOPT_URL, "https://poloniex.com/tradingApi");
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, post_data.length()+1);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, post_data.c_str());
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
}
curl_easy_perform(curl_handle);
curl_easy_cleanup(curl_handle);

return 0;
}

这回到了我身上

{"error":"Invalid command."}

我检查了 hmac 函数结果抛出的网站,它们是平等的。 有人说,加

"Content-Type: application/x-www-form-urlencoded"

可以解决这个问题,但目前还不能。

控制台输出:

> POST /tradingApi HTTP/2
Host: poloniex.com
accept: */*
content-type: application/x-www-form-urlencoded
key: myKey
sign: resultOfHmacFunc
content-length: 40

解决方案是从CURLOPT_POSTFIELDS更改为CURLOPT_COPYPOSTFIELDS