【问题标题】:I cant get the bittrex v3 api in C++ working我无法让 C++ 中的 bittrex v3 api 工作
【发布时间】:2021-01-14 19:12:26
【问题描述】:

在我的一生中,我无法让它发挥作用。我无法找到 Bittrex API v3 的示例代码,所以我将我在其他编程语言中找到的 v1.1 和 v3 的代码拼凑在一起。

我正在使用 curl 和 openssl 库。

我收到“URL 使用错误/非法格式或缺少 URL”错误。

#include "../curl/curl.h"
#include <sha512.hh>
#include "hmac.h"
#include "sha.h"

string bittKey, bittSecret;
string timeStamp, hTTPMethod, uRI, requestBody, contentHash;

CURL *curl;
CURLcode res;

curl = curl_easy_init();

bittKey =       "..."; 
bittSecret =    "...";
timeStamp = std::to_string(time(nullptr));
uRI = "https://api.bittrex.com/v3/balances";
hTTPMethod = "GET";
requestBody = "";
contentHash = sw::sha512::calculate(requestBody);

string preSign = timeStamp + uRI + hTTPMethod + contentHash;
string apiSign = hmac_sha512(preSign, bittSecret);

struct curl_slist* headerList = NULL;

headerList = curl_slist_append(headerList, "Accept: application/json");
headerList = curl_slist_append(headerList, "Content-Type: application/json");
headerList = curl_slist_append(headerList, ("Api-Key: " + bittKey).c_str());
headerList = curl_slist_append(headerList, ("Api - Signature: " + apiSign).c_str());
headerList = curl_slist_append(headerList, ("Api-Timestamp: " + timeStamp).c_str());
headerList = curl_slist_append(headerList, ("Api-Content-Hash: " + contentHash).c_str());

curl_easy_setopt(curl, CURLOPT_URL, uRI);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerList);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);

res = curl_easy_perform(curl);

if (res != CURLE_OK)
    fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

curl_slist_free_all(headerList);
curl_easy_cleanup(curl);

【问题讨论】:

    标签: c++ api curl


    【解决方案1】:

    通过 curl 库搜索和调试了很长时间,解决方案非常简单。

        curl_easy_setopt(curl, CURLOPT_URL, uRI.c_str());
    

    哦,时间戳不像我上面所说的那样工作。它的工作原理如下:

        using namespace std::chrono;
        milliseconds ms = duration_cast<milliseconds>(
            system_clock::now().time_since_epoch()
            );
    
        timeStamp = std::to_string(ms.count());
    

    API 正在运行。

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      相关资源
      最近更新 更多