本文介绍了Binance 加密货币交易所 API “/account"401响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Google Sheet 中的脚本调用 Binance cryptoexchange API 时遇到问题.

I'm having troubles with calling to Binance cryptoexchange API with my script from Google Sheet.

我已经使用示例中的数据检查了我的签名处理https://www.binance.com/restapipub.html#用户内容签名端点安全和我有相同的签名.

I've checked my signature processing with the data from the examplehttps://www.binance.com/restapipub.html#user-content-signed-endpoint-securityand I've got the same signature.

我已经通过另一个中心 (coinigy.com) 检查了我的 API 密钥和秘密,密钥工作正常.

I've checked my API key and secret with another hub (coinigy.com), keys work properly.

但是我自己执行脚本时仍然遇到 401 错误...

But I'm still getting the 401 error executing the script by myself...

币安支持没有回答.

有人可以帮忙吗?

function BinanceTest(key,secret) {
/*var randnumber=Math.random()*500;
Utilities.sleep(randnumber);*/
var baseURL = "https://api.binance.com";
var completeURL = baseURL + "/api/v3/account";
var timestamp=new Date().getTime();
var payload = "timestamp="+timestamp;
var signature = Utilities.computeHmacSha256Signature(payload, secret);
signature = signature.map(function(byte) {
  return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
completeURL=completeURL+"?"+payload+"&signature="+signature;
var params = {
  'method': 'get',
  'headers': {'X-MBX-APIKEY': key},
  'contentType': 'application/x-www-form-urlencoded',
  'muteHttpExceptions': true
};
var response = fetchJSON(completeURL,params);
var servertime=fetchJSON("https://api.binance.com/api/v1/time").serverTime;
return [servertime,timestamp,payload,signature,JSON.stringify(params),completeURL,response];

};


function fetchJSON (url) {
  var randnumber=Math.random()*3000;
  Utilities.sleep(randnumber);
  try {
    var json = UrlFetchApp.fetch(url,{muteHttpExceptions: true })
    } catch (exception) {
      Logger.log(url+": "+exception)
      return 'exception'
    }
  if ('undefined' == typeof(json))
    return 'Error retrieving JSON data'

  if (json.getResponseCode() != 200)
    return json.getResponseCode()

  json = json.getContentText()
  if (json.length<=0)
    return 'JSON data was invalid'

  try {
    json = JSON.parse(json)
  } catch (exception) {
    Logger.log(url+" "+exception)
    return "err2"
  }
  if ('undefined' == typeof(json) || json == null)
    // return 'err'
  return 'Quote data was malformed JSON data'

  return json
}

推荐答案

我认为您的脚本几乎是正确的.但是错误的原因是fetchJSON().fetchJSON() 只接收 url.但是 var response = fetchJSON(completeURL,params); 发送 completeURLparams.这样, fetchJSON() 不会收到 params 并且发生错误.那么这个修改怎么样?

I think that your script is almost correct. But the reason of error is fetchJSON(). fetchJSON() receives only url. But var response = fetchJSON(completeURL,params); sends completeURL and params. By this, fetchJSON() doesn't receive params and the error occurs. So how about this modification?

var response = fetchJSON(completeURL,params);

致:

var response = UrlFetchApp.fetch(completeURL, params);

注意:

  • 如果此修改不起作用,请从 params 中删除 'contentType': 'application/x-www-form-urlencoded',,然后重试.
  • Note :

    • If this modification didn't work, please remove 'contentType': 'application/x-www-form-urlencoded', from params, and try again.
    • 我无法确认这些修改是否有效.所以如果这不起作用,你能告诉我错误信息吗?我想修改.

      I cannot confirm whether these modifications work. So if this didn't work, can you tell me the error messages? I would like to modify.

      请尝试以下示例脚本,并告诉我响应.如果返回当前账户信息"的响应,则表示该脚本有效.

      Please try the following sample script, and tell me the response. If the response which is "current account information" is returned, it means that the script works.

      1. 将以下脚本复制并粘贴到您的脚本编辑器中.
      2. 请在示例脚本中输入您的密钥和密码.
      3. 在脚本编辑器上,运行 -> 运行函数 -> 示例
      4. 脚本完成后,通过查看 -> 日志检索响应

      示例:

      function sample() {
        var key = "### your key ###";
        var secret = "### your secret ###";
      
        var baseURL = "https://api.binance.com";
        var completeURL = baseURL + "/api/v3/account";
        var timestamp=new Date().getTime();
        var payload = "timestamp="+timestamp;
        var signature = Utilities.computeHmacSha256Signature(payload, secret);
        signature = signature.map(function(byte) {
          return ('0' + (byte & 0xFF).toString(16)).slice(-2);
        }).join('');
        completeURL=completeURL+"?"+payload+"&signature="+signature;
        var params = {
          'method': 'get',
          'headers': {'X-MBX-APIKEY': key},
          'muteHttpExceptions': true
        };
        var response = UrlFetchApp.fetch(completeURL, params);
        Logger.log(response.getContentText())
      }
      

      这篇关于Binance 加密货币交易所 API “/account"401响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:53