im using open ssl, cpr and chrono to access krakens restful api to recieve account balance from kraken, but i get a 403 request.
code:
#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;
}
the response i get is:
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>
the rest of the text seems to be krakens logo source(which is like 2 pages long). if you me to include it please let me know!
Well. Error 403, it's on your end. It could be:
baseUrl = "https://api.kraken.com/";
instead of "baseUrl = https://api.kraken.com/0/private/Balance";
, since you already specified the path in rpath
)Trial and error and you'll eventually find the problem.
Options to help:
To debug http/https errors like these, I also recommend (if you have the time) to download some software which can send and help debug your requests (Postman is a good recommendation! Easy to use). Helps plenty when debugging/double-checking issues like the one above.
If you happen to use linux or any sort of terminal which has curl installed, you could try it using the --verbose
parameter (it shows useful info!). Example of use:
curl --verbose --location --request POST 'https://api.kraken.com/0/private/Balance'
--header 'Content-Type: application/x-www-form-urlencoded'
--data-urlencode '... other params if required ...'
--data-urlencode 'API-Key=XXXXXXX'
--data-urlencode 'API-Sign=XXXXXXX'