将以下python代码改写成CC++且不使用binance的头文件:#!usrbinenv pythonimport loggingfrom binanceum_futures import UMFuturesfrom binancelibutils import config_loggingfrom binanceerror import ClientErrorconfig_logginglog
#include
using namespace std; using json = nlohmann::json;
string HMACSHA256(string key, string message) { unsigned char hash[32]; HMAC_CTX hmac_ctx = HMAC_CTX_new(); HMAC_Init_ex(hmac_ctx, key.c_str(), key.length(), EVP_sha256(), NULL); HMAC_Update(hmac_ctx, (unsigned char)message.c_str(), message.length()); unsigned int hash_len = 32; HMAC_Final(hmac_ctx, hash, &hash_len); HMAC_CTX_free(hmac_ctx); stringstream ss; for (int i = 0; i < 32; i++) { ss << hex << setw(2) << setfill('0') << (int)hash[i]; } return ss.str(); }
string get_server_time() {
CURL *curl;
CURLcode res;
string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://fapi.binance.com/fapi/v1/time");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](char *data, size_t size, size_t nmemb, string *writerData) -> size_t {
if(writerData == NULL) {
return 0;
}
writerData->append(data, size * nmemb);
return size * nmemb;
});
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
cerr << "Failed to get server time: " << curl_easy_strerror(res) << endl;
curl_easy_cleanup(curl);
return "";
}
curl_easy_cleanup(curl);
json j = json::parse(readBuffer);
return to_string(j["serverTime"].get
string make_api_request(string endpoint, string query_string, string request_body, string api_key, string secret_key, string method) { CURL *curl; CURLcode res; struct curl_slist *headers = NULL; string timestamp = get_server_time(); string message = "timestamp=" + timestamp; if (method == "GET" && query_string != "") { message += "&" + query_string; } if (method == "POST" && request_body != "") { message += "&" + request_body; } string signature = HMACSHA256(secret_key, message); string url = "https://fapi.binance.com" + endpoint + "?" + message + "&signature=" + signature; curl = curl_easy_init(); if(curl) { headers = curl_slist_append(headers, ("X-MBX-APIKEY: " + api_key).c_str()); if (method == "POST") { headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded"); } curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); if (method == "POST") { curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_body.c_str()); } curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](char *data, size_t size, size_t nmemb, string *writerData) -> size_t { if(writerData == NULL) { return 0; } writerData->append(data, size * nmemb); return size * nmemb; }); string readBuffer; curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); res = curl_easy_perform(curl); if(res != CURLE_OK) { cerr << "Failed to make API request: " << curl_easy_strerror(res) << endl; curl_easy_cleanup(curl); return ""; } curl_easy_cleanup(curl); return readBuffer; } return ""; }
int main() { string api_key = ""; string secret_key = ""; string endpoint = "/fapi/v1/account"; string query_string = "recvWindow=6000"; string request_body = ""; string method = "GET"; string response = make_api_request(endpoint, query_string, request_body, api_key, secret_key, method); cout << response << endl; return 0;
原文地址: http://www.cveoy.top/t/topic/c8MI 著作权归作者所有。请勿转载和采集!