簡體   English   中英

Google Sheets API 調用收到錯誤 RATE_LIMIT_EXCEEDED 並且沒有檢索到我需要的數據

[英]Google sheets API call receives error RATE_LIMIT_EXCEEDED and doesnt retrieve the data i need

運行下面的代碼時,我收到以下錯誤“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

RATE_LIMIT_EXCEEDED 是您要快速進行的防洪。

您需要減慢您發送的請求數量。 檢測錯誤並實現指數回退。 這樣當它失敗時你等待一小段時間然后再試一次,如果它再次失敗你等待一段時間直到 api 讓你再次運行。

所以解決方案是增加更多的延遲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM