我被困在如何根据Google Scripts中的Binance API将signitue正确地包含在我的get命令中。它所说的是
SIGNED端点需要在查询字符串或请求正文中发送附加参数签名。 端点使用HMAC SHA256签名。 HMAC SHA256签名是密钥HMAC SHA256操作。使用secretKey作为密钥,使用totalParams作为HMAC操作的值。 签名不区分大小写。 totalParams定义为与请求主体连接的查询字符串。
我拥有的是:
function BinanceTrades() {
var curTime = Number(new Date().getTime()).toFixed(0)
var sKey = Utilities.computeHmacSha256Signature('symbol=LTCBTC×tamp=' + 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×tamp=" + curTime, {'headers' : headers})
Logger.log(data)
}
我得到的错误是:
{“code”: - 1100,“msg”:“参数'signature'中发现非法字符;合法范围为'^ [A-Fa-f0-9] {64} $'。”}
我不确定如何正确计算HMAC SHA256并合并totalParams。
我以前的帖子是this.
答案 0 :(得分:3)
这些修改怎么样?
"symbol=LTCBTC×tamp=" + curTime
。Utilities.computeHmacSha256Signature()
加密的数据是带符号十六进制的字节数组。反映上述观点的修改后的脚本如下:
function BinanceTrades() {
var key = '**myKey**';
var secret = '**mySeceretKey**';
var curTime = Number(new Date().getTime()).toFixed(0);
var string = "symbol=LTCBTC×tamp=" + 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())
}