John Galassi
John Galassi

Reputation: 318

{msg=Signature for this request is not valid., success=false} error for Binance Api withdraw and deposit request

following this answer on stack I was able to correctly fetch all orders or trades data from my personal binance account.

function connect() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var key = '****';
  var secret = '****';

  var curTime = Number(new Date().getTime()).toFixed(0);
  var symbol = "TRXETH";
  var limit  = 13;
  var string = "symbol="+symbol+"&limit=12&timestamp=" + curTime;
  var sKey = Utilities.computeHmacSha256Signature(string, secret);
  sKey = sKey.map(function(e) {
      var v = (e < 0 ? e + 256 : e).toString(16);
      return v.length == 1 ? "0" + v : v;
  }).join("");
  var params = {
    'method': 'get',
    'headers': {'X-MBX-APIKEY': key},
    'muteHttpExceptions': true
  };
  var url = "https://api.binance.com/api/v3/myTrades?" + string + "&signature=" + sKey;
  var data = UrlFetchApp.fetch(url, params);
  var data = JSON.parse(data.getContentText());

It works just fine but, since I needed to pull also deposit and withdraw data, I thought I'd made a new dedicated script.
And it does not work:

function deposit() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var key = '****';
  var secret = '****';

  var curTime = Number(new Date().getTime()).toFixed(0);
  var string = "/wapi/v3/withdrawHistory.html?timestamp=" + curTime;
  var sKey = Utilities.computeHmacSha256Signature(string, secret);
  sKey = sKey.map(function(e) {
      var v = (e < 0 ? e + 256 : e).toString(16);
      return v.length == 1 ? "0" + v : v;
  }).join("");
  var params = {
    'method': 'get',
    'headers': {'X-MBX-APIKEY': key},
    'muteHttpExceptions': true,
    //'signature': sKey
  };
  var url = "https://api.binance.com" + string + "&signature=" + sKey;
  var data = UrlFetchApp.fetch(url, params);
  var data = JSON.parse(data.getContentText());

now there are several tickets on github for this problem, but none seems to fit my case: can anyone tell me what is wrong with my code? thanks

Github 1 Github 2 Binance Official Documentation

Upvotes: 3

Views: 5835

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • Your tokens for accessing to Binance API is correct.
  • You want to use the method of Withdraw History in Binance API.

I could understand like above from your script and replying. If my understanding is correct, how about the following modification?

Modified script:

From:
var string = "/wapi/v3/withdrawHistory.html?timestamp=" + curTime;
To:
var string = "timestamp=" + curTime;

and

From:
var url = "https://api.binance.com" + string + "&signature=" + sKey;
To:
var url = "https://api.binance.com/wapi/v3/withdrawHistory.html?" + string + "&signature=" + sKey;

Reference:

Upvotes: 2

Related Questions