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
}