Binance API 签名与 Google 脚本

Binance API Signature with Google Scripts

我一直在研究如何根据 Google 脚本中的 Binance API 将符号正确地包含到我的 get 命令中。它说的是

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.

我有的是:

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)
}

我得到的错误是:

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

我不确定如何正确计算 HMAC SHA256 并合并 totalParams。

我之前的post是

这些修改怎么样?

修改点:

来自the manual you provided

  • 在您的例子中,用于签名的字符串是 "symbol=LTCBTC&timestamp=" + curTime
  • 签名为无符号十六进制字符串。
    • 但是在Google Apps Script中,Utilities.computeHmacSha256Signature()加密的数据是带符号的十六进制字节数组。

反映以上几点修改后的脚本如下

修改脚本:

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())
}

注:

  • 关于签名的加密,从the manual you provided.
  • 已经确认这个脚本可以正常工作
  • 我没有 binance.com 的帐户。所以我不能 运行 这个脚本。对不起。
    • 当你运行这个脚本时,如果出现错误,你能告诉我错误信息吗?