Python - Google Calendar API - 服务帐户 - 没有事件列表

时间:2014-12-18 20:28:19

标签: python calendar google-api service-accounts

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

我已经阅读了很多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

1 个答案:

答案 0 :(得分: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对象时在子参数中指定用户帐户的电子邮件地址。例如:

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

Reference