使用Google脚本时,Binance搜索API返回403

发布于 2025-01-28 16:28:19 字数 1187 浏览 5 评论 0原文

我正在使用Binance API来获取USDT的价格。 API在Postman上工作,但在Google脚本上不起作用。

function fetchCryptoPricesFromApi() {
  const data = {
    "page": 1,
    "rows": 10,
    "payTypes": [],
    "asset": "USDT",
    "tradeType": "SELL",
    "fiat": "LKR",
    "publisherType": null,
    "transAmount": "2600"
  }

  const payload = JSON.stringify(data)

  const options = {
    "method" : "POST",
    "contentType" : "application/json",
    "payload" : payload
  }

  let response;
  
  try {
    response = UrlFetchApp.fetch('https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search', options);
  } catch (error) {
    console.log('Oops Error, ', error);
    return
  }

  const prices = JSON.parse(response)['data'];
  console.log(prices)
}

执行此操作时,我会遇到以下错误,

Oops Error,  { [Exception: Request failed for https://p2p.binance.com returned code 403. Truncated server response: <!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" ... (use muteHttpExceptions option to examine full response)] name: 'Exception' }

我也尝试了一些时间。

I'm using the binance API to get the prices of usdt. The API works on postman but it doesn't work on google script.

function fetchCryptoPricesFromApi() {
  const data = {
    "page": 1,
    "rows": 10,
    "payTypes": [],
    "asset": "USDT",
    "tradeType": "SELL",
    "fiat": "LKR",
    "publisherType": null,
    "transAmount": "2600"
  }

  const payload = JSON.stringify(data)

  const options = {
    "method" : "POST",
    "contentType" : "application/json",
    "payload" : payload
  }

  let response;
  
  try {
    response = UrlFetchApp.fetch('https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search', options);
  } catch (error) {
    console.log('Oops Error, ', error);
    return
  }

  const prices = JSON.parse(response)['data'];
  console.log(prices)
}

I get the following error when executing this,

Oops Error,  { [Exception: Request failed for https://p2p.binance.com returned code 403. Truncated server response: <!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" ... (use muteHttpExceptions option to examine full response)] name: 'Exception' }

I tried waiting some time as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2025-02-04 16:28:19

从您的显示错误消息中,我确认了状态代码403ref 在这种情况下,被认为是网站无法直接从Google方面访问。我认为您的问题的原因是归因于此。

在这种情况下,作为解决方法,我想建议对URL访问,而无需直接使用脚本编辑器运行脚本。当我测试此解决方法时,我确认可以返回该值。

在此解决方法中,使用以下流程。

  1. = fetchCryptoPricesFromapi()的自定义函数放在单元格中。
  2. 从API中检索值。
  3. 从单元格中检索值。
  4. 将值解析为JSON数据。

此解决方法的示例脚本如下。

示例脚本:

在此解决方法中,我使用Google电子表格。因此,请创建一个新的Google电子表格,并打开Goog​​le电子表格的脚本编辑器。并且,复制并粘贴以下脚本。并且,使用脚本编辑器运行main()函数。

function fetchCryptoPricesFromApi() {
  const data = {
    "page": 1,
    "rows": 10,
    "payTypes": [],
    "asset": "USDT",
    "tradeType": "SELL",
    "fiat": "LKR",
    "publisherType": null,
    "transAmount": "2600"
  }
  const payload = JSON.stringify(data)
  const options = {
    "method": "POST",
    "contentType": "application/json",
    "payload": payload
  }
  const response = UrlFetchApp.fetch('https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search', options);
  return response.getContentText();
}

// Please run this function.
function main() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const range = sheet.getRange("A1");
  range.setFormula("=fetchCryptoPricesFromApi()");
  SpreadsheetApp.flush();
  const value = range.getValue();
  range.clearContent();
  const prices = JSON.parse(value)['data'];
  console.log(prices)
}

From your showing error message, I confirmed the status code 403. Ref In this case, it is considered that the site cannot be directly accessed from the Google side. I think that the reason for your issue is due to this.

In this case, as a workaround, I would like to propose access to the URL without directly running the script with the script editor. When I tested this workaround, I confirmed that the value could be returned.

In this workaround, the following flow is used.

  1. Put the custom function of =fetchCryptoPricesFromApi() to a cell.
  2. Retrieve the values from the API.
  3. Retrieve the values from the cell.
  4. Parse the value as JSON data.

The sample script of this workaround is as follows.

Sample script:

In this workaround, I use Google Spreadsheet. So please create a new Google Spreadsheet and open the script editor of Google Spreadsheet. And, copy and paste the following script. And, run main() function with the script editor.

function fetchCryptoPricesFromApi() {
  const data = {
    "page": 1,
    "rows": 10,
    "payTypes": [],
    "asset": "USDT",
    "tradeType": "SELL",
    "fiat": "LKR",
    "publisherType": null,
    "transAmount": "2600"
  }
  const payload = JSON.stringify(data)
  const options = {
    "method": "POST",
    "contentType": "application/json",
    "payload": payload
  }
  const response = UrlFetchApp.fetch('https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search', options);
  return response.getContentText();
}

// Please run this function.
function main() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const range = sheet.getRange("A1");
  range.setFormula("=fetchCryptoPricesFromApi()");
  SpreadsheetApp.flush();
  const value = range.getValue();
  range.clearContent();
  const prices = JSON.parse(value)['data'];
  console.log(prices)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文