Google 日历 API: 插入多个事件(在 Python 中)

Google Calendar API: Insert multiple events (in Python)

我正在使用 Google 日历 API,并且已经成功地将单个事件插入到授权的主日历中,但我想对多个事件进行硬编码,这些事件在执行时,将填充用户日历。

例如:

我现在的代码是:

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']))

我需要知道如何将剩余的事件同时添加到日历中。 多个非- 一个 Google 日历事件插入中的重复事件。

不幸的是,事件一次只能执行一个事件,这意味着必须多次调用 events.insert() 方法。要对多个事件进行硬编码,您需要回忆

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

使用不同的 "body" 参数集。

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()

google 日历 "body" 参数专门接受一个事件,并将抛出 400 HTTP 错误,并传入一组事件