Google Calendar API: Insert multiple events (in Py

2019-09-20 11:03发布

问题:

I am using the Google Calendar API, and have successfully managed to insert a single event into an authorized primary calendar, but I would like to hard code multiple events that, when executed, would populate the users calendar.

For example:

  • June 24th 1:00pm "Buy apples"
  • June 26th 3:00pm "Pick up clothes"
  • August 21st 1:00pm "Clean car"
  • September 1st 2:00pm "Return books"

My code right now is:

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=
[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/calendar'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', 
SCOPES)
    creds = tools.run_flow(flow, store, flags) \
          if flags else tools.run(flow, store)
CAL = build('calendar', 'v3', http=creds.authorize(Http()))

GMT_OFF = '-04:00'          # ET/MST/GMT-4
EVENT = {
    'summary': 'Buy apples',
    'start': {'dateTime': '2017-06-24T13:00:00%s' % GMT_OFF},
    'end': {'dateTime': '2017-06-24T14:00:00%s' % GMT_OFF},
}

e = CAL.events().insert(calendarId='primary',
                        sendNotifications=True, body=EVENT).execute()

print('''*** %r event added:
    Start: %s
    End: %s''' % (e['summary'].encode('utf-8'),
                  e['start']['dateTime'], e['end']['dateTime']))

I need to know how to add the remaining events, as an example, to the calendar at the same time. Multiple non-recurring events within one Google Calendar event insert.

回答1:

Unfortunately events can only be executed one event at a time, which means the events.insert() method must be called multiple times. To hard code several events you need to recall

    e = CAL.events().insert(calendarId='primary',
                    sendNotifications=True, body=EVENT).execute()

with a different "body" parameter set.

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=
[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/calendar'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json',
SCOPES)
    creds = tools.run_flow(flow, store, flags) \
          if flags else tools.run(flow, store)
CAL = build('calendar', 'v3', http=creds.authorize(Http()))

GMT_OFF = '-04:00'          # ET/MST/GMT-4
EVENT1 = {
    'summary': 'Buy apples',
    'start': {'dateTime': '2017-06-24T13:00:00%s' % GMT_OFF},
    'end': {'dateTime': '2017-06-24T14:00:00%s' % GMT_OFF},
}
EVENT2 = {
    'summary': 'Buy Bannanas',
    'start': {'dateTime': '2017-06-25T13:00:00%s' % GMT_OFF},
    'end': {'dateTime': '2017-06-25T14:00:00%s' % GMT_OFF},
}
EVENT3 = {
    'summary': 'Buy candy',
    'start': {'dateTime': '2017-06-26T13:00:00%s' % GMT_OFF},
    'end': {'dateTime': '2017-06-26T14:00:00%s' % GMT_OFF},
}

e = CAL.events().insert(calendarId='primary',
                sendNotifications=True, body=EVENT1).execute()
e = CAL.events().insert(calendarId='primary',
                sendNotifications=True, body=EVENT2).execute()
e = CAL.events().insert(calendarId='primary',
                sendNotifications=True, body=EVENT3).execute()

The google calendar "body" parameter takes in specifically a single event and will thrown a 400 HTTP error with an array of events passed in