英文:
why do i get 403 request when trying to get available balance from kraken C++
问题
我使用OpenSSL、CPR和Chrono来访问Kraken的RESTful API以获取账户余额,但我收到了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() {
// 变量
string key = "XXXXXXXXXXX";
string secret = "XXXXXXXXXXXXXXX";
string baseUrl = "https://api.kraken.com/0/private/Balance";
// 获取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;
// 获取签名
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请求
Response r;
Header header;
header.insert({ "API-Key", key });
header.insert({ "API-Sign", signature });
r = Post(Url{ baseUrl }, Body{ postData }, Header{ header });
cout << "状态码: " << r.status_code << endl;
cout << r.text << endl;
}
我收到的响应是:
状态码: 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>
剩下的文本似乎是Kraken的标志源代码(大约有2页长)。如果需要,请告诉我。
英文:
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!
答案1
得分: 3
Error 403,这是您的问题。可能是以下原因之一:
- 密钥可能已过期
- URL 不正确。这是情况(
baseUrl = "https://api.kraken.com/"
而不是baseUrl = "https://api.kraken.com/0/private/Balance";
,因为您已经在rpath
中指定了路径) - Https 通信需要证书。我假设您正确使用了 openssl 库。但您可以检查一下。
不断尝试,最终会找到问题。
帮助选项:
-
为了调试类似这样的 http/https 错误,我还建议(如果您有时间的话)下载一些可以发送和帮助调试请求的软件(Postman 是一个很好的建议!易于使用)。在调试/双重检查上述问题等问题时非常有帮助。
-
如果您恰好使用 Linux 或任何带有安装 curl 的终端,可以尝试使用
--verbose
参数(它显示有用的信息!)。使用示例:curl --verbose --location --request POST 'https://api.kraken.com/0/private/Balance'
--header 'Content-Type: application/x-www-form-urlencoded'
--data-urlencode '... 如果需要其他参数 ...'
--data-urlencode 'API-Key=XXXXXXX'
--data-urlencode 'API-Sign=XXXXXXX';
英文:
Well. Error 403, it's on your end. It could be:
- The key could have expired
- The URL is not correct. Which was the case (
baseUrl = "https://api.kraken.com/";
instead of"baseUrl = https://api.kraken.com/0/private/Balance";
, since you already specified the path inrpath
) - Https communication requires a certificate. I assume you're using the openssl library correctly. But you might check that.
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'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论