简体   繁体   English

Google Sheets UrlFetchApp 达到外部 API 速率限制

[英]External API rate limits are hit by Google Sheets UrlFetchApp

I have a Google Sheets spreadsheet with data that is retrieved from an external API via URLFetchApp :我有一个 Google 表格电子表格,其中包含通过URLFetchApp从外部 API 检索到的数据:

function callExternalAPI(endpoint) {
  const url = baseUrl + endpoint;
  const response = UrlFetchApp.fetch(url, {
    "muteHttpExceptions": true,
    "headers": {
      "X-Schema-Version": "latest",
      "Authorization": "Bearer " + apiKey
    }
  });

  return response;
}

This function is called a couple of times whenever the user manually presses a refresh button that I put in a custom menu:每当用户手动按下我放在自定义菜单中的刷新按钮时,都会调用此 function 几次:

在 Google 表格上预览外部菜单

In total, each refresh should perform no more than a dozen or so API calls.总的来说,每次刷新应该执行不超过十几个 API 调用。

Important note: The API I'm using has a rate limit of 600 requests per IP per minute.重要说明:我使用的 API 的速率限制为每 IP 每分钟 600 个请求。

When I (based in Malta) refresh the API data myself, everything works fine.当我(位于马耳他)自己刷新 API 数据时,一切正常。 However, when a different user (based in the Phillipines) tries to do the same thing, the external API responds with an error message stating that the rate limit is hit.然而,当不同的用户(位于菲律宾)尝试做同样的事情时,外部 API 响应一条错误消息,指出达到了速率限制。

I do not have access to the server-side configuration of the external API.我无权访问外部 API 的服务器端配置。

Is there any way to work around this?有什么办法可以解决这个问题吗? The user is not able to use the spreadsheet at all because of the rate limit.由于速率限制,用户根本无法使用电子表格。

All requests made from UrlFetchApp.fetch() originate from Google's servers.UrlFetchApp.fetch()发出的所有请求都源自 Google 的服务器。 Google leverages a fixed set of IP addresses per region for Apps Script. Google 在每个区域为 Apps 脚本利用一组固定的 IP 个地址。 So, if a lot of scripts from different user accounts (that are restricted to the same pool of IP addresses) in the same region are hitting that API, chances are the 600 request IP quota will be exhausted.因此,如果同一区域中来自不同用户帐户(仅限于同一 IP 地址池)的大量脚本都在命中 API,则 600 个请求 IP 的配额可能会用完。

I imagine the Phillipines to be one of the more active locations, with a high volume of devs in the region.我认为菲律宾是较为活跃的地区之一,该地区拥有大量开发人员。 So there is unlikely to be an App-Script based solution for your issue.因此,您的问题不太可能有基于 App-Script 的解决方案。

However, if you can afford it (both in terms of cost and time/effort to learn), you can leverage different cloud technologies, such as Google Cloud Functions.但是,如果您负担得起(在成本和学习时间/精力方面),您可以利用不同的云技术,例如 Google Cloud Functions。 They typically have dynamic IPs but you can get a static IP address unique to a Cloud Function, so you won't run into any issues with exhausting the API quota.它们通常具有动态 IP,但您可以获得云 Function 独有的 static IP 地址,因此您不会遇到用尽 API 配额的任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM