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

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

问题描述:

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

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.

例如:

  • 6月24日1:00 pm买苹果"
  • 6月26日3:00 pm收拾衣服"
  • 8月21日1:00 pm清洁车"
  • 9月1日2:00 pm还书"

我现在的代码是:

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.

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

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

设置了不同的"body"参数.

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

google Calendar的"body"参数专门接受一个事件,并且会通过传入的事件数组引发400 HTTP错误

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