【问题标题】:Google sheets API call receives error RATE_LIMIT_EXCEEDED and doesnt retrieve the data i needGoogle 表格 API 调用收到错误 RATE_LIMIT_EXCEEDED 并且没有检索到我需要的数据
【发布时间】:2021-10-02 13:26:28
【问题描述】:

运行下面的代码时,我收到以下错误“RATE_LIMIT_EXCEEDED”。我添加了一些延迟,但不确定我还能做些什么来纠正这个问题。有谁知道如何解决这个问题?

> Exception has occurred: HttpError <HttpError 429 when requesting
> https://sheets.googleapis.com/v4/spreadsheets/1FJiVwQu2AAca06dpkoh20zQYGyyQ8RBZJ8xWZgGlcS4/values/form%20%20-%20v1.01%211%3A200?alt=json
> returned "Quota exceeded for quota metric 'Read requests' and limit
> 'Read requests per minute per user' of service 'sheets.googleapis.com'
> for consumer 'project_number:1010550314584'.". Details: "[{'@type':
> 'type.googleapis.com/google.rpc.ErrorInfo', 'reason':
> 'RATE_LIMIT_EXCEEDED', 'domain': 'googleapis.com', 'metadata':
> {'quota_metric': 'sheets.googleapis.com/read_requests', 'consumer':
> 'projects/1010550314584', 'quota_limit':
> 'ReadRequestsPerMinutePerUser', 'service':
> 'sheets.googleapis.com'}}]">   File
> "OEE.py", line 43, in
> fetch_data_from_google_spreadsheet
>     range=RANGE_NAME).execute())   File "Forge.py", line 43, in <module>
>     forge_OEE = OEE.fetch_data_from_google_spreadsheet('blhalblahblha',google_credentials=creds)


# gets data from google spreadsheet 
def fetch_data_from_google_spreadsheet(SPREADSHEET_ID, google_credentials):
    
    # looks at connecting to the google api service 
    # If modifying these scopes, delete the file token.pickle.
    SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']


    service = build('sheets', 'v4', credentials=google_credentials)

    # Call the Sheets API
    sheet = service.spreadsheets()
    
    # Returns all sheets (tabs) in a spread sheet
        time.sleep(3)
    return_sheet_ID = sheet.get(spreadsheetId=SPREADSHEET_ID, ranges=None, includeGridData=None, x__xgafv=None).execute()

    # extracts just the sheets 
    individual_sheets = return_sheet_ID.get("sheets")

    list_of_dict_sheets = []

    # loops through each tab and returns the rows within each sheet 
    for item in individual_sheets:
        # returns the sheet names to pass into sheet values
        RANGE_NAME = item.get("properties").get("title") + "!" + "1" + ":" + str(item.get("properties").get("gridProperties").get("rowCount"))
        time.sleep(3)
        # returns the rows within the spreadsheets 
        list_of_dict_sheets.append (sheet.values().get(spreadsheetId=SPREADSHEET_ID,
                                range=RANGE_NAME).execute())
    df2 = None 
    # loops through each sheet and returns the details  
    for each_sheet in list_of_dict_sheets:
        df = pd.DataFrame(each_sheet.get("values"))
        df = df.rename(columns=df.iloc[0])
        df = df.iloc[1:]
        df2 = pd.concat([df,df2])
    return df2

【问题讨论】:

    标签: python google-api google-sheets-api google-api-python-client


    【解决方案1】:

    RATE_LIMIT_EXCEEDED 是防洪措施,您将要禁食。

    您需要减慢发送请求的数量。检测错误并实施指数退避。这样当它失败时你等待一小段时间然后再试一次,如果再次失败你再等一会儿,直到 api 让你再次运行。

    所以解决办法是增加更多的延迟。

    【讨论】:

    • 我读过它,但不确定它是什么意思。当错误被捕获时,我是否添加了 60 秒、120 秒和 340 秒等的延迟...
    • 我通常从 10 seconds , 20 , 40 , 80 , 160 开始,然后每次加倍。
    猜你喜欢
    • 1970-01-01
    • 2019-09-21
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多