将以下python代码改写成CC++且不使用binance的头文件:import loggingfrom binanceum_futures import UMFuturesfrom binancelibutils import config_loggingfrom binanceerror import ClientErrorconfig_logginglogging loggingDEBUGk
#include
using namespace std;
string api_key = ""; string secret_key = "";
string get_current_time() { time_t t = time(nullptr); tm* local_time = localtime(&t); stringstream ss; ss << local_time->tm_year + 1900 << "-" << local_time->tm_mon + 1 << "-" << local_time->tm_mday << "T" << local_time->tm_hour << ":" << local_time->tm_min << ":" << local_time->tm_sec << "Z"; return ss.str(); }
string hmac_sha256(string key, string message) { unsigned char hash[32]; unsigned char* hmac_result = HMAC(EVP_sha256(), (const unsigned char*) key.c_str(), key.length(), (const unsigned char*) message.c_str(), message.length(), hash, nullptr); string result = ""; char buf[3]; for (int i = 0; i < 32; i++) { sprintf(buf, "%02x", hmac_result[i]); result += buf; } return result; }
string send_request(string url, string query_string) { CURL* curl; CURLcode res; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); struct curl_slist* headers = nullptr; headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "X-MBX-APIKEY: " + api_key); headers = curl_slist_append(headers, "X-MBX-REQUEST-TIME: " + get_current_time()); headers = curl_slist_append(headers, "X-MBX-SIGNATURE: " + hmac_sha256(secret_key, query_string)); string response; if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, ""); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](char* ptr, size_t size, size_t nmemb, void* data) -> size_t { ((string*) data)->append(ptr, size * nmemb); return size * nmemb; }); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); res = curl_easy_perform(curl); if (res != CURLE_OK) { cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << endl; } curl_easy_cleanup(curl); curl_global_cleanup(); } return response; }
int main() { string symbol = "BTCUSDT"; string recvWindow = "6000"; string url = "https://fapi.binance.com/fapi/v1/userTrades"; string query_string = "symbol=" + symbol + "&recvWindow=" + recvWindow; string response = send_request(url + "?" + query_string, query_string); cout << response << endl; return 0;
原文地址: https://www.cveoy.top/t/topic/da73 著作权归作者所有。请勿转载和采集!