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

标签 python google-api google-calendar-api

非常感谢您抽出时间来提供帮助!

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

  • 创建新日历;将其命名为“XYZ”(而不是“主”日历)
  • 插入硬编码自定义事件
  • 能够根据需要删除整个“XYZ”日历以及后续插入的事件

我在将新日历插入经过验证的日历时遇到问题。 我浏览了谷歌日历资源,但似乎根本无法遵循。我现在的代码如下:

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

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”中。另外,我想添加根据用户请求从经过验证的 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将事件添加到新创建的日历中。

关于python - 在插入事件之前插入新日历 Google API 和删除日历事件(Python 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44310175/

相关文章:

python - 忽略一次测试的类级别补丁装饰器

python - 如何从 Python 中的嵌套字典中打印所有键和值?

python - numpy - 从具有间距的数组中选择元素

python - split 函数返回意外结果

javascript - 使用 Angular 在页面加载时访问 Google Maps API

google-maps - Google Maps API v3 将 map 重新​​定位到标记

ios - iOS 内置日历应用程序如何与各种日历源同步?

node.js - 通过 google http rest api 发送电子邮件

java - Google 日历 API 共享示例

google-api - 将事件添加到 Google 日历不起作用