使用 Google Scripts 的 Binance API 签名 [英] Binance API Signature with Google Scripts
问题描述
我被困在如何根据 Binance API<正确地将签名包含到我的 get 命令中/a> 在 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 端点需要在查询字符串或请求正文中发送附加参数签名.端点使用 HMAC SHA256 签名.HMAC SHA256 签名是加密的 HMAC SHA256 操作.使用您的 secretKey 作为密钥,使用 totalParams 作为 HMAC 操作的值.签名不区分大小写.totalParams 定义为与请求正文连接的查询字符串.
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×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}$'."}
{"code":-1100,"msg":"Illegal characters found in parameter 'signature'; legal range is '^[A-Fa-f0-9]{64}$'."}
我不确定如何正确计算 HMAC SHA256 并合并 totalParams.
I am unsure of how to compute the HMAC SHA256 correctly and incorporate the totalParams.
我之前的帖子是这个.
推荐答案
这些修改怎么样?
来自您提供的手册
- 在您的情况下,用于签名的字符串是
"symbol=LTCBTC×tamp=" + curTime
. - 签名是未签名的十六进制字符串.
- 但在 Google Apps Script 中,由
Utilities.computeHmacSha256Signature()
加密的数据是已签名的十六进制字节数组.
- In your case, the string which is used for the signature is
"symbol=LTCBTC×tamp=" + 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.
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()) }
注意:
- 关于签名的加密,已经从您提供的手册中确认该脚本可以正常工作一>.
- 我没有 binance.com 的帐户.所以我无法运行这个脚本.抱歉.
- 当你运行这个脚本时,如果出现错误,你能告诉我错误信息吗?
这篇关于使用 Google Scripts 的 Binance API 签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- But at Google Apps Script, the data which was encrypted by
- 但在 Google Apps Script 中,由