Binance cryptoexchange API&"/帐户&"401回应 [英] Binance cryptoexchange API "/account" 401 response

查看:100
本文介绍了Binance cryptoexchange API&"/帐户&"401回应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google表格中的脚本调用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 example https://www.binance.com/restapipub.html#user-content-signed-endpoint-security and 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); 发送 completeURL params .这样, 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 cryptoexchange API&amp;"/帐户&amp;"401回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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