gpt4 book ai didi

javascript - 使用谷歌脚本的 Binance API 签名

转载 作者:行者123 更新时间:2023-11-29 18:58:07 27 4
gpt4 key购买 nike

我一直在研究如何根据 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。

我之前的帖子是 this.

最佳答案

这些修改怎么样?

修改点:

来自 the manual you provided

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

反射(reflect)以上几点修改后的脚本如下。

修改后的脚本:

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 的帐户。所以我无法运行这个脚本。对不起。
    • 当你运行这个脚本时,如果发生错误,你能告诉我错误信息吗?

关于javascript - 使用谷歌脚本的 Binance API 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48191838/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com