使用Google脚本时,Binance搜索API返回403
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的显示错误消息中,我确认了状态代码
403
。 ref 在这种情况下,被认为是网站无法直接从Google方面访问。我认为您的问题的原因是归因于此。在这种情况下,作为解决方法,我想建议对URL访问,而无需直接使用脚本编辑器运行脚本。当我测试此解决方法时,我确认可以返回该值。
在此解决方法中,使用以下流程。
= fetchCryptoPricesFromapi()
的自定义函数放在单元格中。此解决方法的示例脚本如下。
示例脚本:
在此解决方法中,我使用Google电子表格。因此,请创建一个新的Google电子表格,并打开Google电子表格的脚本编辑器。并且,复制并粘贴以下脚本。并且,使用脚本编辑器运行
main()
函数。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.
=fetchCryptoPricesFromApi()
to a cell.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.