【发布时间】:2021-03-17 02:50:10
【问题描述】:
因此,我在 Google Cloud Compute 引擎中设置了一个 python 脚本,该脚本设置为在一天中定期使用 cron 选项卡运行。但是最近,脚本返回这个错误,
gspread.exceptions.APIError: {'code': 429, 'message': "配额指标“读取请求”超出配额,并限制服务“sheets.googleapis”的“每用户每分钟读取请求”。 com' for consumer 'project_number:583704109550'.", 'status': 'RESOURCE_EXHAUSTED', 'details': [{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': ' RATE_LIMIT_EXCEEDED','域':'googleapis.com','元数据':{'quota_metric':'sheets.googleapis.com/read_requests','quota_limit':'ReadRequestsPerMinutePerUser','服务':'sheets.googleapis.com ', '消费者': 'projects/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)
【问题讨论】:
-
您的谷歌云帐户对您每分钟可以执行的读取次数有限制,而您已超过该限制。
标签: python google-sheets google-cloud-platform