将 python 用于 google 日历监视请求

Using python for google calendar watch-requests

我想为 google 日历设置监视请求,使用 python (无需设置单独的域)。

  1. 我导入了api客户端,可以成功获取认证凭证,如下例:https://developers.google.com/google-apps/calendar/quickstart/python

  2. 然后我设置了一个日历服务,我可以毫无问题地列出、插入和删除事件。

我遇到的问题是当我执行监视请求以便从 python 中获得一个 webhook 时。 我收到错误: "googleapiclient.errors.HttpError: https://www.googleapis.com/calendar/v3/calendars/primary/events/watch?alt=json returned "WebHook 回调必须是 HTTPS:">"

很明显,我遗漏了一些需要设置的东西,以便日历对我提供的 webhook 感到满意。 是否可以从 python 中执行此操作,而无需使用 https 设置单独的域,如果可以,如何操作?

最小工作示例:


import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import uuid

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Calendar API'


def get_credentials():

    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')

    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'calendar-api.json')

    store = Storage(credential_path)
    credentials = store.get()

    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: 
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials


def main():

    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)


## TESTING callback receiver: 
    eventcollect = {
        'id': str(uuid.uuid1()),
        'type': "web_hook"
    }

    service.events().watch(calendarId='primary', body=eventcollect).execute()


if __name__ == '__main__':
    main()

watch requests的文档中,有一个Required Properties部分。这部分明确指出:

  • 设置为 URL 的 address 属性 字符串用于侦听和响应此通知渠道的通知。 这是您的 Webhook 回调 URL,它必须使用 HTTPS

抱歉,我认为没有解决方法。