为什么我会收到配额限制错误?谷歌云平台计算引擎虚拟机

因此,我在 Google Cloud Compute 引擎中设置了一个 python 脚本,该脚本设置为全天定期使用 cron 选项卡运行。但是最近,脚本返回了这个错误,

gspread.exceptions.APIError: {'code': 429, 'message': "配额指标 '读取请求' 超出配额,并限制消费者 'project_number' 服务 'sheets.googleapis.com' 的'每用户每分钟读取请求数' :583704109550'.", 'status': 'RESOURCE_EXHAUSTED', 'details': [{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'RATE_LIMIT_EXCEEDED', 'domain' : 'googleapis.com', 'metadata': {'quota_metric': 'sheets.googleapis.com/read_requests', 'quota_limit': 'ReadRequestsPerMinutePerUser', 'service': 'sheets.googleapis.com', 'consumer': '项目/583704109550'}}]}

我试图研究为什么它会抛出它,但由于我在该领域缺乏经验而不知所措。这是任何人都想知道的脚本,

from woocommerce import API
from df2gspread import df2gspread as d2g
from df2gspread import gspread2df as g2d
from collections import defaultdict
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
def parseData(entry):
        #Extract relectant information (all key value pairs that are listed under meta_data)
        d = {pair["key"]:pair["value"] for pair in entry["line_items"][0]["meta_data"]}
        d["Event"] = entry["line_items"][0]["name"]
        return d
wcapi = API(
    url="REDACTED",
    consumer_key="REDACTED",
    consumer_secret="REDACTED",
    version="wc/v3",
    query_string_auth="true"
)
entries = []
pageNum = 1
while True:
        #API is paginated with products per page limit of 100
        rawEntries = wcapi.get("orders", params = {"per_page": 100, "page": pageNum}).json()
        #Page until there are no entries on a page
        if len(rawEntries) == 0:
                break
        entries.extend([parseData(e) for e in rawEntries])
        pageNum += 1
rawEvents = defaultdict(list)
for entry in entries:
        #Organize entries by their event
        rawEvents[entry["Event"]].append(entry)
events = {k: pd.DataFrame(v).fillna('') for (k,v) in rawEvents.items()} #Built a dataframe for each event
#Upload to Google Sheets using gspread and df2gspread
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('/home/shahav2016/GoogleKeyJSON.json', scope)
gc = gspread.authorize(credentials)
spreadsheet_key = 'REDACTED FOR PRIVACY REASONS' #The name of the spreadsheet - look at the URL
for event, df in events.items():
        #Filter out test events
        if event not in ['REDACTED FOR PRIVACY']:
                #Upload the data to the correct sheet
                d2g.upload(df, spreadsheet_key, event, row_names = False, credentials = credentials)

回答

Google Sheets API 对每 100 秒可以执行的请求数有限制。这是来自Sheets API v4的文档页面。

此版本的 Google Sheets API 限制为每个项目每 100 秒 500 个请求,每个用户每 100 秒 100 个请求。读取和写入的限制是单独跟踪的。没有每日使用限制。

要查看或更改项目的使用限制,或请求增加配额,请执行以下操作:

如果您还没有项目的结算帐号,请创建一个。访问 API 控制台中 API 库的启用 API 页面,然后从列表中选择一个 API。要查看和更改与配额相关的设置,请选择配额。要查看使用情况统计信息,请选择使用情况。

此外,有三个选项可以解决此限制:

  1. 增加quota limit您在开发者控制台中调用的 Sheets API。
  2. 由于您使用的是 for 循环,因此请求发送速度非常快。我认为sleep在该循环中的某个位置放置一个是明智的,这样您就不会每 100 秒超过 100 个请求。
  3. 如果事情变得太慢,请尝试一次将多个更改上传到电子表格,此机制称为batching。这也将减少 API 请求的数量。

以上是为什么我会收到配额限制错误?谷歌云平台计算引擎虚拟机的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>