我使用开放ssl,cpr和chrono访问krakens restful api从kraken接收帐户余额,但我得到一个403请求。
代码:
#include <iostream>
#include <string>
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
#include <chrono>
#include <openssl/sha.h>
#include <openssl/hmac.h>
using namespace std;
using namespace cpr;
using namespace chrono;
using json = nlohmann::json;
int main(){
//variables
string key = "XXXXXXXXXXX";
string secret = "XXXXXXXXXXXXXXX";
string baseUrl =
"https://api.kraken.com/0/private/Balance";
//get nonce
const auto p1 = system_clock::now();
int T1 = duration_cast<seconds>(p1.time_since_epoch()).count();
string timestamp = to_string(t1);
string postData = "nonce=" + timestamp;
//get signature
string rPath = "/0/private/Balance";
string message = rpath + timestamp + postData;
unsigned char hmac_result[EVP_MAX_MD_SIZE];
unsigned int hmac_result_len;
HMAC(
EVP_sha256(),
secret.c_str(), secret.length(),
(unsigned char*)message.c_str(), message.length(),
hmac_result, &hmac_result_len
);
string signature = string(reinterpret_cast<char*>(hmac_result),
hmac_result_len);
//cpr request
Response r;
Header header;
header.insert({ "API-Key", key });
header.insert({ "API-Sign", signature });
r = Post(Url{ baseUrl }, Body{ postData }, Header{ header });
cout << "Status Code: " << r.status_code << endl;
cout << r.text << endl;
}
我得到的答复是:
Status Code: 403
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="robots" content="noindex" />
<title>That is Not Allowed</title>
<style type="text/css">body,html{height:100%;width:100%}body{background-color:#0d0c52;color:#fff;font-family:Helvetica,sans-serif;margin:0}article{display:grid;grid-template-columns:100%;grid-template-rows:auto 1fr auto;min-height:100%}footer,header{background-color:#5740d9;padding:34px 0 30px 40px}main{align-items:center;display:inline-grid;grid-template-columns:50% 50%;padding:50px 0}main>div:first-child{padding-left:30%}@media (max-width:800px){main{grid-template-columns:100%}main>div:first-child{padding:0 40px}main>div:last-child{display:none}}a{color:#fff}</style>
</head>
其余的文字似乎是krakens标志源(这是像2页长)。如果你我包括它,请让我知道!
1条答案
按热度按时间mbskvtky1#
好吧。错误403,是你的电话。可能是:
baseUrl = "https://api.kraken.com/";
而不是"baseUrl = https://api.kraken.com/0/private/Balance";
,因为您已经在rpath
中指定了路径)反复试验,你最终会找到问题的。
帮助选项:
--verbose
参数来尝试一下(它显示了有用的信息!)。curl --详细--位置--请求POST 'https://api.kraken.com/0/private/Balance'