Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

javascript - Binance API Signature with Google Scripts

I am stuck on how to correctlly include the signitue into my get command based off of the Binance API within Google Scripts. What it states is

SIGNED endpoints require an additional parameter, signature, to be sent in the query string or request body. Endpoints use HMAC SHA256 signatures. The HMAC SHA256 signature is a keyed HMAC SHA256 operation. Use your secretKey as the key and totalParams as the value for the HMAC operation. The signature is not case sensitive. totalParams is defined as the query string concatenated with the request body.

What I have is:

function BinanceTrades() {
  var curTime = Number(new Date().getTime()).toFixed(0)
  var sKey = Utilities.computeHmacSha256Signature('symbol=LTCBTC&timestamp=' + curTime, '**mySeceretKey**');
  Logger.log(sKey)
  var headers = {'X-MBX-APIKEY': '**myKey**'}
  var data = UrlFetchApp.fetch("https://api.binance.com/api/v3/allOrders?signature=" + sKey + "&symbol=LTCBTC&timestamp=" + curTime, {'headers' : headers})
  Logger.log(data)
}

and the error I get is:

{"code":-1100,"msg":"Illegal characters found in parameter 'signature'; legal range is '^[A-Fa-f0-9]{64}$'."}

I am unsure of how to compute the HMAC SHA256 correctly and incorporate the totalParams.

My previous post was this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about these modifications?

Modification points :

From the manual you provided

  • In your case, the string which is used for the signature is "symbol=LTCBTC&timestamp=" + curTime.
  • The signature is the string of the unsigned hexadecimal.
    • But at Google Apps Script, the data which was encrypted by Utilities.computeHmacSha256Signature() is the bytes array of the signed hexadecimal.

The modified script which reflected above points is as follows.

Modified script :

function BinanceTrades() {
  var key = '**myKey**';
  var secret = '**mySeceretKey**';

  var curTime = Number(new Date().getTime()).toFixed(0);
  var string = "symbol=LTCBTC&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/allOrders?" + string + "&signature=" + sKey;
  var data = UrlFetchApp.fetch(url, params);
  Logger.log(data.getContentText())
}

Note :

  • About the encryption of signature, it has already confirmed that this script works fine from the manual you provided.
  • I have no account for binance.com. So I couldn't run this script. I'm sorry.
    • When you run this script, if the error occurs, can you show me the error messages?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...