我尝试从Kraken C++获取可用余额时为什么收到403请求?

huangapple go评论64阅读模式
英文:

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 &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cpr/cpr.h&gt;
#include &lt;nlohmann/json.hpp&gt;
#include &lt;chrono&gt;
#include &lt;openssl/sha.h&gt;
#include &lt;openssl/hmac.h&gt;


using namespace std;
using namespace cpr;
using namespace chrono;
using json = nlohmann::json;


int main(){

    //variables
    string key = &quot;XXXXXXXXXXX&quot;;
	string secret = &quot;XXXXXXXXXXXXXXX&quot;;
    string baseUrl =  
             &quot;https://api.kraken.com/0/private/Balance&quot;;

    //get nonce
	const auto p1 = system_clock::now();
	int T1 = duration_cast&lt;seconds&gt;(p1.time_since_epoch()).count();
	string timestamp = to_string(t1);
    string postData = &quot;nonce=&quot; + timestamp;

    //get signature
	string rPath = &quot;/0/private/Balance&quot;;
	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, &amp;hmac_result_len
	);

	string signature = string(reinterpret_cast&lt;char*&gt;(hmac_result), 
                              hmac_result_len);


    //cpr request
    Response r;

	Header header;
	header.insert({ &quot;API-Key&quot;, key });
	header.insert({ &quot;API-Sign&quot;, signature });

    r = Post(Url{ baseUrl }, Body{ postData }, Header{ header });
	cout &lt;&lt; &quot;Status Code: &quot; &lt;&lt; r.status_code &lt;&lt; endl;
	cout &lt;&lt; r.text &lt;&lt; endl;
}

the response i get is:

Status Code: 403
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;meta
      name=&quot;viewport&quot;
      content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;
    /&gt;
    &lt;meta name=&quot;robots&quot; content=&quot;noindex&quot; /&gt;
    &lt;title&gt;That is Not Allowed&lt;/title&gt;
    &lt;style type=&quot;text/css&quot;&gt;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&gt;div:first-child{padding-left:30%}@media (max-width:800px){main{grid-template-columns:100%}main&gt;div:first-child{padding:0 40px}main&gt;div:last-child{display:none}}a{color:#fff}&lt;/style&gt;
  &lt;/head&gt;

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 = &quot;https://api.kraken.com/&quot;; instead of &quot;baseUrl = https://api.kraken.com/0/private/Balance&quot;;, since you already specified the path in rpath)
  • 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'

huangapple
  • 本文由 发表于 2023年3月4日 00:59:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定