如何为我的日历获取特定日期的所有 google 日历事件?

How to take ALL the google calendars events for a particular day for my calnedar?

我想在 linux 终端和 return 文本中用一个简单的脚本列出 我自己的 日常事件。输出应该是这样的:

    (all day) Buy a pony to my neighbor's daughter 
    (all day) Douglas Adams birthday
    (all day) Battle of Mborore commemoration
    8:00am-8:30am take dog to church
    9:00am-11:30am meeting with Papa Snurf
    (...)
    6:00pm-11:00pm Python 4.0 meetup (free pizza!)
    11:20pm Lunar eclipse and end of the world

正如您想象的那样,当我访问 "today" 时,这来自我在一个 Web 界面中拥有的多个日历。当我使用 android 应用程序或 Web 界面时,它对我来说是透明的,但我无法从 python.

找到一个简单的方法来做到这一点

我期待一些 iCal URL 和来自 Google 日历的一些标记。但似乎并非如此,我需要某种 OAuth2 身份验证(根据 https://developers.google.com/api-client-library/python/ 上的 "easy authentication"),就像我代表其他人做某事一样(这可能是最常见的用途适用于应用程序开发人员的案例)。

谁能给我指出一个 python 执行类似操作的完整脚本?

编辑一个:阅读评论后我用 Python 编写了一些代码:

from datetime import datetime, timedelta
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

with open('my-app.p12', 'rb') as f:
  key = f.read()

service_account_name = '1....3@developer.gserviceaccount.com'
sub = 'MY_EMAIL'

credentials = SignedJwtAssertionCredentials(
    service_account_name,
    key, 
    scope=['https://www.googleapis. com/auth/calendar',
        'https://www.googleapis. com/auth/calendar.readonly'],
    sub=sub)

http = httplib2.Http()
http = credentials.authorize(http)

service = build(serviceName='calendar', version='v3', http=http)

showDeleted = True

lists = service.calendarList().list().execute()
pprint.pprint(lists)

page_token = None
while True:
  events = service.events().list(calendarId=service_account_name,
    pageToken=page_token).execute()

service_account_name 取自 Oauth -> 服务帐户 -> 电子邮件地址(在开发人员控制台的凭据下),my-app.p12 是我在创建应用程序时下载的密钥二进制文件(我在同一过程中的对话框中也有一个文本密码,但从未使用过。)

注意:URL 上有一个 SPACE... 因为我在堆栈溢出中没有足够的声誉

编辑 2:已解决

一些注意事项:创建一个服务帐户。就像@DaImTo 指出的那样。我不知何故错过了他在链接上指出的一些解释

特别是我错过了在此处为域创建正确的范围身份验证: https://admin.google.com/AdminHome?chromeless=1#OGX:ManageOauthClients

非常感谢您的回答。我希望我有更多的声誉来支持那里的一些答案。

查阅这些资源:

  • 日历示例 API v3 程序 (link)
  • Google 日历 v3 API 文档 (link)
  • 如何通过 Google 开发者控制台创建客户端机密文件 (link)

更新:尝试与用户共享您的日历1....3@developer.gserviceaccount.com,看看是否能解决未经授权的客户端问题。

因为您只能访问自己的数据,所以我建议您查看 Service account。通过像任何其他用户一样授予服务帐户电子邮件地址访问您的日历的权限,它将能够读取事件,而无需您使用 Oauth2。

之后就是使用的问题了

日历列表:list to get a list of all of the calendars you have given the service account access to. Once it has a list of the calendars it can then find the events

这应该足以让您入门。尝试搜索 Ruby Google 日历 api 示例。不幸的是,我没有使用 ruby 的经验,所以无法帮助您。