1

几周以来,我一直在尝试让我的 Google 日历活动通过终端上的 Python 脚本运行。我的目标是从特定时间(昨天)提取我的事件,更改事件的名称,并用新名称保存它们。

我已经阅读了许多 Stack Overflow 页面和大量 API 文档,甚至问过我的 Python 老师(我正在上夜校),但没有任何运气。

下面的代码“工作”并产生一堆东西,包括一个空的“项目”列表[]。我怀疑我实际上并没有连接到我的日历,因为理论上列表应该是我所有的东西;那或者我比我想象的要差得多。

无论如何,这是我拥有的代码,它以两种方式产生不希望的结果。预先感谢您的任何帮助!

import pprint
import pytz
import httplib2
import requests

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

with open('calendarchecker.p12', 'rb') as f:
  key = f.read()

service_account_name = #EMAIL ADDRESS IN OAUTH SERVICE ACCOUNT

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

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()
  pprint.pprint(events)
  for event in events['items']:
    print event['summary']
  page_token = events.get('nextPageToken')
  if not page_token:
    break
4

1 回答 1

0
credentials = SignedJwtAssertionCredentials(
    service_account_name,
    key, 
    scope=['https://www.googleapis.com/auth/calendar',
        'https://www.googleapis.com/auth/calendar.readonly'],
    sub='<userid to impersonate>')

如果您已委派了对服务帐户的域范围访问权限并且您想模拟用户帐户,请在创建 Credentials 对象时在 sub 参数中指定用户帐户的电子邮件地址。例如:

credentials = SignedJwtAssertionCredentials(client_email, private_key,
    'https://www.googleapis.com/auth/sqlservice.admin',
    sub='user@example.org')

参考

于 2014-12-22T11:40:24.377 回答