2

我有一个可以通过脚本编辑器正常工作的 Google 表格脚本。当我尝试通过菜单选项运行它时,它给了我来自 API 的“请求被阻止”。该脚本基本上是在尝试访问 Binance API。

这是代码:

function getMyTrades() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var thresholdVal = parseFloat(ss.getSheetByName("Settinngs").getRange("C1").getValue());
  var balancesSheet = ss.getSheetByName("Account Overview");
  var openOrdersSheet = ss.getSheetByName("OpenOrders");

  var api = "/api/v3/account";
  var json = getBinanceResponse(api,"");    
  var balancesArr =  json['balances'];
 // remaining code here..... 
}


function getBinanceResponse(api,string){
  var key = 'key_here';
  var secret = 'secret_key here';
  var timestamp = Number(new Date().getTime()).toFixed(0);
  string += "timestamp=" + timestamp;
  var baseUrl = "https://api.binance.com";
  var signature = Utilities.computeHmacSha256Signature(string, secret);

  signature = signature.map(function(e) {
    var v = (e < 0 ? e + 256 : e).toString(16);
    return v.length == 1 ? "0" + v : v;
  }).join("");

  var query = "?" + string + "&signature=" + signature;

  var params = {
    'method': 'get',
    'headers': {'X-MBX-APIKEY': key},
    'muteHttpExceptions': true,
  };

  var data = UrlFetchApp.fetch(baseUrl + api + query, params);
  Logger.log(data)

  return JSON.parse(data);
}

这是请求被阻止时币安的响应

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Request blocked.

<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: Ov-DKv4UDH88eYZbo366xy7BsdsJVVcvHxLLPLeS2li6VqXw1qp05K4fw==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>
4

0 回答 0