C++ https post故障c++;
我尝试访问Binance API的端点 我需要将其作为post订单发送,并在头中使用API密钥和请求正文中的参数 <我的代码是用C++编写的,我使用的是LIcCURL.< hmac_sha256()函数也在工作 我有以下代码:C++ https post故障c++;,c++,curl,http-post,C++,Curl,Http Post,我尝试访问Binance API的端点 我需要将其作为post订单发送,并在头中使用API密钥和请求正文中的参数
bool BinanceAPI::make_Withdrawal(string api_key , string secret_key ,string asset , string address , double amount , string &id ){
cout << "BINANCE: MAKE WITHDRAWAL"<<endl;
ArbUtils au;
Json::Value result;
CURL * hand;
bool ret = false;
hand = curl_easy_init();
string url = "https://api.binance.com/wapi/v3/withdraw.html";
string post_data("asset=");
post_data.append( asset);
post_data.append("&address=");
post_data.append(address);
post_data.append( "&amount" );
post_data.append( to_string(amount) );
post_data.append( "×tamp=" );
post_data.append( to_string(au.get_msTime() ) );
string signature = hmac_sha256( secret_key.c_str() , post_data.c_str());
post_data.append( "&signature=" );
post_data.append( signature );
vector<string>extra_http_header;
string header_chunk("X-MBX-APIKEY: ");
header_chunk.append( api_key );
extra_http_header.push_back( header_chunk );
string str_result;
curl_easy_setopt(hand , CURLOPT_URL , url.c_str() );
curl_easy_setopt(hand , CURLOPT_WRITEFUNCTION , BinanceAPI::callback);
curl_easy_setopt(hand , CURLOPT_WRITEDATA , &str_result );
curl_easy_setopt(hand , CURLOPT_SSL_VERIFYPEER , false);
curl_easy_setopt(hand , CURLOPT_ACCEPT_ENCODING , "gzip" );
curl_easy_setopt(hand , CURLOPT_POST , 1);
struct curl_slist *chunk = NULL;
for(int i= 0 ; i < extra_http_header.size() ; i++){
chunk = curl_slist_append(chunk , extra_http_header[i].c_str() );
}
curl_easy_setopt(hand , CURLOPT_HTTPHEADER , chunk);
curl_easy_setopt(hand , CURLOPT_POSTFIELDS , post_data.c_str() );
CURLcode res;
res = curl_easy_perform(hand);
curl_easy_cleanup(hand);
}
bool BinanceAPI::提取(字符串api密钥、字符串密钥、字符串资产、字符串地址、双倍金额、字符串和id){
cout我已经面临同样的问题好几天了。刚刚修复。我的代码是python tho。希望你能参考它
import time, hmac, hashlib, urllib, json, requests
key = "my key"
secret_key = "my sk"
asset = "BTC"
addr = "abcdef" #btc address
amount = 1
timestamp = int(time.time() * 1000) #timestamp in ms
#the request parameters
request_body = {"asset": asset, "address": addr, \
"amount": amount, "timestamp": timestamp}
#sign the request parameters with the secret key
#urllib.parse.urlencode(request_body) convert request body to a query string format
signature = hmac.new(bytes(secret_key, "utf-8"), \
bytes(urllib.parse.urlencode(request_body), "utf-8"), \
hashlib.sha256).digest().hex()
#add the signature to the request parameters
request_body["signature"] = signature
header = {"X-MBX-APIKEY": key, \
"Content-Type": "application/x-www-form-urlencoded"}
#append the query string to the url
url = "https://api.binance.com/wapi/v3/withdraw.html?" +\
urllib.parse.urlencode(request_body)
response = requests.post(url, headers=header) #send post request
print(response.json())
问题来自于糟糕的文档。终结点需要post请求,但如果单独发送请求正文,则会失败,如果发送的是查询字符串,则会正常工作。希望能有所帮助。我已经面临同样的问题好几天了。刚刚得到修复。我的代码是python tho。希望您能参考它
import time, hmac, hashlib, urllib, json, requests
key = "my key"
secret_key = "my sk"
asset = "BTC"
addr = "abcdef" #btc address
amount = 1
timestamp = int(time.time() * 1000) #timestamp in ms
#the request parameters
request_body = {"asset": asset, "address": addr, \
"amount": amount, "timestamp": timestamp}
#sign the request parameters with the secret key
#urllib.parse.urlencode(request_body) convert request body to a query string format
signature = hmac.new(bytes(secret_key, "utf-8"), \
bytes(urllib.parse.urlencode(request_body), "utf-8"), \
hashlib.sha256).digest().hex()
#add the signature to the request parameters
request_body["signature"] = signature
header = {"X-MBX-APIKEY": key, \
"Content-Type": "application/x-www-form-urlencoded"}
#append the query string to the url
url = "https://api.binance.com/wapi/v3/withdraw.html?" +\
urllib.parse.urlencode(request_body)
response = requests.post(url, headers=header) #send post request
print(response.json())
问题出在糟糕的文档中。端点需要post请求,但如果单独发送请求正文,则会失败;如果发送查询字符串,则会正常工作。希望能有所帮助