我在使用新的 Google Calendar API v3 python 库时遇到问题。文档似乎有点稀疏。我可以对特定日历进行身份验证并获取事件。但是,我想尽可能使用 gdata 库执行批量更新:
# example from gdata
# feed that holds all the batch rquest entries
request_feed = gdata.calendar.data.CalendarEventFeed()
# add the update entries to the batch feed
request_feed.AddUpdate(entry=updateEntry1)
request_feed.AddUpdate(entry=updateEntry2)
# submit the batch request to the server
response_feed = self.cal_client.ExecuteBatch(request_feed, gdata.calendar.client.DEFAULT_BATCH_URL)
这里有一个示例 https://developers.google.com/google-apps/calendar/batch#example html。但是我可以使用python库来做到这一点吗?
这里有
通用的Google API Python库批处理说明。尝试类似操作:
from apiclient.http import BatchHttpRequest
def insert_event(request_id, response, exception):
if exception is not None:
# Do something with the exception
pass
else:
# Do something with the response
pass
service = build('calendar', 'v3')
batch = BatchHttpRequest(callback=insert_event)
batch.add(service.events().quickAdd(calendarId="you@domain.com",
text="Lunch with Jim on Friday"))
batch.add(service.events().quickAdd(calendarId="you@domain.com",
text="Dinner with Amy on Saturday"))
batch.add(service.events().quickAdd(calendarId="you@domain.com",
text="Breakfast with John on Sunday"))
batch.execute(http=http)
另一种方式,使用 Calendar API 中的 new_batch_http_request 方法:
event = {
'summary': 'Google I/O 2015',
'description': 'A chance to hear more about google.',
'start': {
'dateTime': '2021-05-28T09:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': '2021-05-28T17:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
}
event2 = {
'summary': 'Rocky Balboa',
'description': 'more products.',
'start': {
'dateTime': '2021-05-22T09:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': '2021-05-22T17:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
}
from googleapiclient.http import BatchHttpRequest
import httplib2
from googleapiclient import discovery
batch = service_calendar.new_batch_http_request()
batch.add(service_calendar.events().insert(calendarId=calendar_id, body=event2))
batch.add(service_calendar.events().insert(calendarId=calendar_id, body=event))
batch.execute()
这是对我有用的:
batch = service.new_batch_http_request()
for event_id in list_of_events:
batch.add(service.events().delete(eventId=event_id, calendarId=calendar_id), callback=callback)
batch.add(service.events().insert(body=api_format, calendarId=calendar_id))
batch.execute()
下面是创建服务的完整代码:
import os
import pickle
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from pathlib import Path
SCOPES = ['https://www.googleapis.com/auth/calendar.events',
'https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/spreadsheets.readonly']
def authorize(json_path='credentials_oauth2.json', token_path='token.pickle', scopes=SCOPES):
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(token_path):
with open(token_path, 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
json_path, SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open(token_path, 'wb') as token:
pickle.dump(creds, token)
return creds
def get_calendar_service(json_path, token_path):
creds = authorize(json_path, token_path)
service = build('calendar', 'v3', credentials=creds, cache_discovery=False)
return service
JSON_PATH = Path(r"../credentials/credentials.json")
TOKEN_PATH = Path(r"../credentials/token.pickle")
service = get_calendar_service(JSON_PATH, TOKEN_PATH)