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

时间:2017-06-01 14:24:12

标签: python google-api google-calendar-api

非常感谢你花时间帮忙!

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

  • 创建新日历;称之为" XYZ" (而不是"主要"日历)
  • 插入硬编码自定义事件
  • 能够删除整个" XYZ"日历和随后插入的事件(如果需要)

我无法在新版日历中插入新日历。 我查看了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'。另外,我想添加删除日历的功能' XYZ'根据用户请求从一个经过验证的Gmail。

再次感谢您的帮助!如果我能提供更清晰的信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

您需要添加使用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将事件添加到新创建的日历中。