我一直在尝试使用curl在c++中使用deribit api 这是我获取访问令牌的代码
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* userp) {
userp->append((char*)contents, size * nmemb);
return size * nmemb;
}
class DeribitAuth {
private:
std::string apiUrl;
std::string clientId;
std::string clientSecret;
CURL* curl;
public:
DeribitAuth(const std::string& id, const std::string& secret)
: apiUrl("https://test.deribit.com/api/v2/public/auth"),
clientId(id),
clientSecret(secret) {
std::cout << clientId << " " << clientSecret << std::endl;
// Initialize CURL
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
}
~DeribitAuth() {
if (curl) {
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
std::pair<bool,std::string> authenticate() {
if (!curl) {
std::cerr << "Failed to initialize CURL" << std::endl;
return {false, "Failed" };
}
// Create JSON payload
Json::Value root;
root["jsonrpc"] = "2.0";
root["id"] = 9929;
root["method"] = "public/auth";
Json::Value params;
params["grant_type"] = "client_credentials";
params["client_id"] = clientId;
params["client_secret"] = clientSecret;
/* params["scope"]= "sco "connection mainaccount"*/
root["params"] = params;
// Convert JSON to string
Json::FastWriter writer;
std::string jsonString = writer.write(root);
// Response string
std::string response;
// Set up CURL options
curl_easy_setopt(curl, CURLOPT_URL, apiUrl.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonString.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
// Set headers
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Perform the request
CURLcode res = curl_easy_perform(curl);
// Clean up headers
curl_slist_free_all(headers);
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
return {false,"Failed"};
}
// Parse response
try {
Json::Value jsonResponse;
Json::Reader reader;
bool parsingSuccessful = reader.parse(response, jsonResponse);
if (!parsingSuccessful) {
std::cerr << "Failed to parse response" << std::endl;
return { false,"Failed" };;
}
// Check for errors in response
if (jsonResponse.isMember("error")) {
std::cerr << "API Error: " << jsonResponse["error"].toStyledString() << std::endl;
return { false,"Failed" };;
}
/* std::string s = jsonResponse["result"]["access_token"].asString();
std::cout << s << std::endl;*/
// Print successful response
std::cout << "Access token: " << jsonResponse << std::endl;
return {true,jsonResponse["result"]["access_token"].asString()};
}
catch (const std::exception& e) {
std::cerr << "Exception while parsing response: " << e.what() << std::endl;
return { false,"Failed" };
}
}
};
这样我就可以获得访问令牌。但是当我使用访问令牌使用此代码发出购买请求时
std::string makeRequest(const std::string& endpoint, const std::string& params, const std::string& method = "GET") {
std::string url = base_url + endpoint;
std::string response;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
struct curl_slist* headers = NULL;
std::string str_token = "Authorization: Bearer " + access_token;
const char* token = str_token.c_str();
headers=curl_slist_append(headers, token);
headers=curl_slist_append(headers, "Content-Type: application/json");
struct curl_slist* temp = headers;
std::cout << "Header List" <<std:: endl;
while (temp != nullptr)
{
std::cout << temp->data << std::endl;
temp = temp->next;
}
std::cout << "Header List end" << std::endl;
if (method == "POST") {
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params.c_str());
}
CURLcode res = curl_easy_perform(curl);
curl_slist_free_all(headers);
if (res != CURLE_OK) {
throw std::runtime_error("Failed to make request: " + std::string(curl_easy_strerror(res)));
}
return response;
}
我期望得到正确的响应,但它给出了意外的错误。
兄弟,我还需要 Deribit API C++ 方面的帮助,你能帮助我吗?