使用Google脚本的Binance API签名 [英] Binance API Signature with Google Scripts

查看:462
本文介绍了使用Google脚本的Binance API签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持如何正确地将signitue包含到我的get命令中,基于 Binance API 在Google脚本中。它表示的是


SIGNED端点需要在查询字符串或请求主体中发送一个额外的参数签名。
端点使用HMAC SHA256签名。 HMAC SHA256签名是一个键控HMAC SHA256操作。使用您的secretKey作为key和totalParams作为HMAC操作的值。
签名不区分大小写。
totalParams被定义为与请求体连接的查询字符串。


我拥有的是:

 函数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:在参数'signature'中发现非法字符;合法范围是'^ [A-Fa-f0-9] {64 }

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



我以前的帖子是这个

解决方案

这些修改如何?

修改点:



手册提供




  • 在您的情况下,用于签名的字符串是symbol = LTCBTC& amp ; timestamp =+ curTime

  • 签名是无符号十六进制的字符串。


    • 但在Google Apps脚本中,由 Utilities.computeHmacSha256Signature()加密的数据是字节数组。




上面反映的修改过的脚本是如下。

修改后的脚本:



 函数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 = {$ b $'method':'get',
'headers':{'X-MBX-APIKEY':key},$ b $'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帐号。所以我无法运行这个脚本。对不起。


    • 当您运行此脚本时,如果发生错误,您能向我显示错误消息吗?



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.

解决方案

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?

这篇关于使用Google脚本的Binance API签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆