Reputation: 155
I'm having trouble using the new Google Calendar API v3 python library. The documentation seems a bit sparse. I can authenticate and get events on a specific calendar. However, I would like to perform batch update as was possible with the gdata library:
# 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)
There is an example here https://developers.google.com/google-apps/calendar/batch#example in html. But can I do it using the python library?
Upvotes: 3
Views: 2413
Reputation: 1870
This is what worked for me:
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()
Here's the complete code to create the service:
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)
Upvotes: 0
Reputation: 56
Another way, using the new_batch_http_request method in the Calendar API:
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()
Upvotes: 1
Reputation: 13528
There's generic Google API Python library batch instructions here. Try something like:
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="[email protected]",
text="Lunch with Jim on Friday"))
batch.add(service.events().quickAdd(calendarId="[email protected]",
text="Dinner with Amy on Saturday"))
batch.add(service.events().quickAdd(calendarId="[email protected]",
text="Breakfast with John on Sunday"))
batch.execute(http=http)
Upvotes: 4