python Google Calendar API Python快速入门

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Google Calendar API Python快速入门相关的知识,希望对你有一定的参考价值。

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

import datetime
import calendar

SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'AAA'
SERVICE_ACCOUNT_ID = "xxx@xxx.com"
CALENDAR_ID= ['aaa@aaa',
              bbb@bbb,
              ccc@ccc
            ]

def get_credentials_OAuth2():
    store = Storage(CLIENT_SECRET_FILE)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        credentials = tools.run(flow, store)
    return credentials

def get_credentials_serviceAccount():
    scopes = 'https://www.googleapis.com/auth/calendar'
    credentials = ServiceAccountCredentials.from_json_keyfile_name('key.json',scopes)
    return credentials

  
def lambda_handler(event, context):
    credentials = get_credentials_OAuth2()
#     credentials = get_credentials_serviceAccount()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)

    d = datetime.date.today()
    weekday = d.weekday()
    if weekday == calendar.SUNDAY:
        print(d)
    else:
        delta = datetime.timedelta(7 - (calendar.SUNDAY - weekday))
        d = d - delta

    timeMin = d
    timeMax = d + datetime.timedelta(7)
    timeMin = timeMin.isoformat() + 'T' + '00:00:00.000000' + 'Z'
    timeMax = timeMax.isoformat() + 'T' + '00:00:00.000000' + 'Z'

    eventsResult = []
    for id in CALENDAR_ID:
        e = service.events().list(
            calendarId=id, timeMin=timeMin, timeMax=timeMax, maxResults=50, singleEvents=True,
            orderBy='startTime').execute()
        eventsResult += e.get('items', [])
    return eventsResult

if __name__ == '__main__':
    events = lambda_handler('','')
    for event in events:
        if event['start'].get('dateTime', False):
            print(event['start']['dateTime'], event['end']['dateTime'], event['summary'], event['organizer'].get('displayName'))
        else:
            print(event['start']['date'], event['end']['date'], event['summary'], event['organizer'].get('displayName'))

以上是关于python Google Calendar API Python快速入门的主要内容,如果未能解决你的问题,请参考以下文章

使用 Google Calendar .Net SDK 从 Google Calendar Event 中删除 Google Meet 链接

无法验证服务器身份calendar.google

如何通过 Javascript 中的 Google Calendar API 获取 Google Calendar 中事件的位置?

Google 日历 API 范围 - calendar.readonly 和 calendar.events.readonly 之间有啥区别?

Google Calendar API 批量请求 PHP

Google Calendar API,如何添加带有附加新生成的google meet的事件?