如何解决我通过Google Calendar API不断收到“超出费用限制”的信息-不知道如何减少请求量了
每次运行脚本时,都会出现“超出速率限制”错误:
<HttpError 403 when requesting https://www.googleapis.com/calendar/v3/calendars/calednar-url/events/12345abcd? returned "Rate Limit Exceeded">
我已更改它,因此我在请求之间进行批处理并延迟1秒,以减少请求的数量,但仍然没有效果。
您能指导我解决该问题的方法吗?
该脚本的目的是将一群人的假期从主假期日历复制到仅开发者日历。
脚本每次运行时,都会从主日历获得上一年的每个假期,从开发日历中删除每个事件,然后仅将开发假期添加回开发日历。可能可以对其进行优化,但不确定哪种方法最好。
import json
import os.path
import pickle
from datetime import datetime
from time import sleep
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
CONFIG_PATH = os.path.join(DIR_PATH,'config.json')
TOKEN_PICKLE_PATH = os.path.join(DIR_PATH,'token.pickle')
CREDENTIALS_PATH = os.path.join(DIR_PATH,'credentials.json')
def get_config():
with open(CONFIG_PATH,'rb') as f:
return json.load(f)
def get_credentials():
credentials = None
if os.path.exists(TOKEN_PICKLE_PATH):
with open(TOKEN_PICKLE_PATH,'rb') as f:
credentials = pickle.load(f)
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
CREDENTIALS_PATH,['https://www.googleapis.com/auth/calendar.events']
)
credentials = flow.run_console()
with open(TOKEN_PICKLE_PATH,'wb') as f:
pickle.dump(credentials,f)
return credentials
def clear_dev_calendar(service,config):
old_dev_holidays = get_calendar_events(service,config['calendar_ids']['dev'])
batch = service.new_batch_http_request(callback=callback)
for holiday in old_dev_holidays:
batch.add(
service.events().delete(
calendarId=config['calendar_ids']['dev'],eventId=holiday['id']
)
)
batch.execute()
sleep(1)
def add_dev_holidays(service,config):
all_holidays = get_calendar_events(service,config['calendar_ids']['holiday'])
dev_holidays = []
for holiday in all_holidays:
try:
name = holiday['summary'].split('-')[1].strip()
except (IndexError,KeyError):
continue
if name in config['names']:
dev_holidays.append({
'summary': holiday['summary'],'start': {'date': holiday['start']['date']},'end': {'date': holiday['end']['date']}
})
batch = service.new_batch_http_request(callback=callback)
for holiday in dev_holidays:
batch.add(
service.events().insert(
calendarId=config['calendar_ids']['dev'],body=holiday
)
)
batch.execute()
sleep(1)
def get_calendar_events(service,calendar_id):
start_time = f'{datetime.now().year - 1}-01-01T00:00:00Z'
events = []
page_token = None
while True:
page_events = service.events().list(
calendarId=calendar_id,pageToken=page_token,timeMin=start_time
).execute()
sleep(1)
events += page_events['items']
page_token = page_events.get('nextPageToken')
if not page_token:
break
return events
def callback(request_id,response,exception):
if exception:
raise exception
def main():
config = get_config()
service = build('calendar','v3',credentials=get_credentials())
clear_dev_calendar(service,config)
add_dev_holidays(service,config)
if __name__ == '__main__':
main()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。