Egorische
Egorische

Reputation: 23

Binance cryptoexchange API "/account" 401 response

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

Answers (1)

Tanaike
Tanaike

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?

From :

var response = fetchJSON(completeURL,params);

To :

var response = UrlFetchApp.fetch(completeURL, params);

Note :

  • If this modification didn't work, please remove '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.

Edit :

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.

  1. Copy and paste following script to your script editor.
  2. Please input your key and secret in the sample script.
  3. On the script editor, Run -> Run function -> sample
  4. After the script was finished, retrieve the response by View -> Logs

Sample :

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

Related Questions