在插入事件 Google API 和删除日历事件之​​前插入新日历(在 Python 中)

Insert New Calendar Before Inserting Events Google API and Delete Calendar Events (in Python)

非常感谢您花时间提供帮助!

我正在创建一个 Python 脚本,我需要执行以下操作:

我在向 oauthed 日历中插入新日历时遇到问题。 我查看了 Google 日历资源,但似乎根本无法理解。我现在的代码如下:

注意:由于我不知道如何插入新日历,事件目前编码进入主日历

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': 'Find venue',
     'start': {'dateTime': '2017-06-23T13:00:00%s' % GMT_OFF},
     'end': {'dateTime': '2017-06-23T14:00:00%s' % GMT_OFF},
}
 EVENT2 = {
     'summary': 'Visit museum',
     'start': {'dateTime': '2017-07-09T13:00:00%s' % GMT_OFF},
     'end': {'dateTime': '2017-07-09T14:00:00%s' % GMT_OFF},
}

 EVENT3 = {
     'summary': 'Buy fruits',
     'start': {'dateTime': '2017-07-23T13:00:00%s' % GMT_OFF},
     'end': {'dateTime': '2017-07-23T14:00:00%s' % GMT_OFF},
}

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

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

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

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

因此,在插入日历事件之​​前,我想插入一个名为 'XYZ' 的完整新日历。然后,将编码的以下事件添加到 'XYZ'。另外,我想添加根据用户请求从 oauthed Gmail 中删除日历 'XYZ' 的功能。

再次感谢您的帮助!让我知道是否可以提供更清晰的信息。

您需要添加使用 calendars().insert() method.

的代码
new_calendar = {
    'summary': 'XYZ',
    'timeZone': 'America/Los_Angeles'
}

created_calendar = CAL.calendars().insert(body=new_calendar).execute()

print created_calendar['id']

然后可以使用来自 created_calendar 的 ID 将事件添加到新创建的日历中。