Reputation: 23
I'm having troubles with calling to Binance cryptoexchange API with my script from Google Sheet.
I've checked my signature processing with the data from the example https://www.binance.com/restapipub.html#user-content-signed-endpoint-security and I've got the same signature.
I've checked my API key and secret with another hub (coinigy.com), keys work properly.
But I'm still getting the 401 error executing the script by myself...
Binance support doesn't answer.
Can anybody help?
function BinanceTest(key,secret) {
/*var randnumber=Math.random()*500;
Utilities.sleep(randnumber);*/
var baseURL = "https://api.binance.com";
var completeURL = baseURL + "/api/v3/account";
var timestamp=new Date().getTime();
var payload = "timestamp="+timestamp;
var signature = Utilities.computeHmacSha256Signature(payload, secret);
signature = signature.map(function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
completeURL=completeURL+"?"+payload+"&signature="+signature;
var params = {
'method': 'get',
'headers': {'X-MBX-APIKEY': key},
'contentType': 'application/x-www-form-urlencoded',
'muteHttpExceptions': true
};
var response = fetchJSON(completeURL,params);
var servertime=fetchJSON("https://api.binance.com/api/v1/time").serverTime;
return [servertime,timestamp,payload,signature,JSON.stringify(params),completeURL,response];
};
function fetchJSON (url) {
var randnumber=Math.random()*3000;
Utilities.sleep(randnumber);
try {
var json = UrlFetchApp.fetch(url,{muteHttpExceptions: true })
} catch (exception) {
Logger.log(url+": "+exception)
return 'exception'
}
if ('undefined' == typeof(json))
return 'Error retrieving JSON data'
if (json.getResponseCode() != 200)
return json.getResponseCode()
json = json.getContentText()
if (json.length<=0)
return 'JSON data was invalid'
try {
json = JSON.parse(json)
} catch (exception) {
Logger.log(url+" "+exception)
return "err2"
}
if ('undefined' == typeof(json) || json == null)
// return 'err'
return 'Quote data was malformed JSON data'
return json
}
Upvotes: 2
Views: 2532
Reputation: 201378
I think that your script is almost correct. But the reason of error is fetchJSON()
. fetchJSON()
receives only url
. But var response = fetchJSON(completeURL,params);
sends completeURL
and params
. By this, fetchJSON()
doesn't receive params
and the error occurs. So how about this modification?
var response = fetchJSON(completeURL,params);
var response = UrlFetchApp.fetch(completeURL, params);
'contentType': 'application/x-www-form-urlencoded',
from params
, and try again.I cannot confirm whether these modifications work. So if this didn't work, can you tell me the error messages? I would like to modify.
Please try the following sample script, and tell me the response. If the response which is "current account information" is returned, it means that the script works.
function sample() {
var key = "### your key ###";
var secret = "### your secret ###";
var baseURL = "https://api.binance.com";
var completeURL = baseURL + "/api/v3/account";
var timestamp=new Date().getTime();
var payload = "timestamp="+timestamp;
var signature = Utilities.computeHmacSha256Signature(payload, secret);
signature = signature.map(function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
completeURL=completeURL+"?"+payload+"&signature="+signature;
var params = {
'method': 'get',
'headers': {'X-MBX-APIKEY': key},
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(completeURL, params);
Logger.log(response.getContentText())
}
Upvotes: 1