首页 > 解决方案 > importJSON 从服务器返回错误代码 429(速率受限),而我不是

问题描述

我正在使用 Brad Jasper 和 Trevor Lohrbeer ( https://github.com/bradjasper/ImportJSON )的未修改版本的 ImportJSON 库

使用这个库,我正在尝试将加密货币价格导入 Google 表格,如下所示:

importJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin",
"0.current_price")

供您参考,这是给定端点的 JSON 响应:

[
    {
        "id":"bitcoin",
        "symbol":"btc",
        "name":"Bitcoin",
        "image":"https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
        "current_price":49101,
        "market_cap":917650396614,
        "market_cap_rank":1,
        "fully_diluted_valuation":1029890733176,
        "total_volume":64288903539,
        "high_24h":49826,
        "low_24h":46682,
        "price_change_24h":289.93,
        "price_change_percentage_24h":0.59398,
        "market_cap_change_24h":2244251078,
        "market_cap_change_percentage_24h":0.24516,
        "circulating_supply":18711362.0,
        "total_supply":21000000.0,
        "max_supply":21000000.0,
        "ath":64805,
        "ath_change_percentage":-24.58992,
        "ath_date":"2021-04-14T11:54:46.763Z",
        "atl":67.81,
        "atl_change_percentage":71969.04088,
        "atl_date":"2013-07-06T00:00:00.000Z",
        "roi":null,
        "last_updated":"2021-05-16T07:57:37.155Z"
    }
]

我期待返回49101,但得到一个错误:

Exception: Request failed for https://api.coingecko.com returned code 429. 
Truncated server response: error code: 1015 (use muteHttpExceptions option to 
examine full response (line 220).

我做了一些谷歌搜索,error 429似乎意味着你从你的 IP 查询 API 的次数太多了。来自 Coingecko 网站:

4.2 Rate limit for the CoinGecko API is 10 calls each second per Internet Protocol 
(“IP”) address, although such rate limit may be varied by CoinGecko at any time 
in its sole discretion without notice or reference to you or any Users

但这不适用于我。首先,在过去的一小时和第二次中,我只进行了大约 50 次 API 调用(试图让它工作),如果我在浏览器中访问端点,我会毫无问题地得到这个 JSON 响应,告诉我我' m 真的没有限速。

我想进一步调试,但不明白如何“使用 muteHttpExceptions 选项检查完整响应”。我的importJSON电话会是什么样子?我在下面尝试过,但它没有改变返回的错误:

=importJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin","0.current_price", "muteHttpExceptions=true")

有谁知道我做错了什么调用muteHttpExceptions或者为什么我得到这个错误返回?有人可以运行相同的公式,看看他们是否得到相同的错误?

标签: javascriptjsongoogle-sheets

解决方案


更简单

=currentPrice("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin")

用这个两行脚本

function currentPrice(url) {
  var data = JSON.parse(UrlFetchApp.fetch(url).getContentText())
  return data[0].current_price
}

在您恢复在 coingecko 中获取数据的权利之后。“简单是终极的复杂性。” (史蒂夫·乔布斯)


推荐阅读