3

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

John Galassi
  • 309
  • 2
  • 16
  • 2
    Can you explain the detail information of about `it does not work`? By the way, I think that in your script, the endpoint is not correct. So can I ask you what method of Binance API you want to use? – Tanaike Dec 10 '19 at 22:34
  • it means that when I try to connect with the first function it retrieves correctly all the data; whit the second one (deposit()) it gives the error reported in title, i.e.: "{msg=Signature for this request is not valid., success=false}". I need to use GET if that's what you mean. I hope I understood correctly, otherwise feel free to ask even in more detail if needed. thanks – John Galassi Dec 11 '19 at 00:15
  • 1
    Thank you for replying. From your replying, it is found that your tokens for accessing to Binance API is correct. And from your script, I noticed that you want to use the method of Withdraw History in Binance API. In this case, how about the following modification? Unfortunately, I cannot test the modified script. So if that didn't resolve your issue, please show the error message. I would like to check the script again. – Tanaike Dec 11 '19 at 00:29

1 Answers1

2
  • 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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • thanks it works! Can I ask you what's the reason behind this change? I also have been reading the page you referenced but didn't understand how you got to that solution. ps, what about ```'contentType':'application/x-www-form-urlencoded'```: it works both with or without, what does that imply if it doesnt take too long to explain? thanks – John Galassi Dec 11 '19 at 00:48
  • 1
    @John Galassi Thank you for replying and testing it. I'm glad your issue was resolved. The default content type is `application/x-www-form-urlencoded` for UrlFetchApp. [Ref](https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl,-params) So it works for both with and without. And from your question, I could know that your above script works. So I could modify your below script like that. – Tanaike Dec 11 '19 at 00:53