2017-04-19 35 views
0

我想使用python (不設置單獨的域)設置谷歌日曆的觀看請求。使用python進行谷歌日曆觀看請求

  1. 我導入的API客戶端,並能順利拿到認證證書,下面的例子:https://developers.google.com/google-apps/calendar/quickstart/python

  2. 然後我設置了一個日曆服務,並且我能夠列出,插入和刪除沒有任何問題的事件。

我遇到的問題是當我執行一個監視請求,以便我有一個從蟒蛇內的webhook。 我收到錯誤: 「googleapiclient.errors.HttpError:https://www.googleapis.com/calendar/v3/calendars/primary/events/watch?alt=json returned」WebHook callback must be 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() 

回答

0

watch requests文檔,有一個Required Properties一部分。在這種情況下它是明確指出:

  • 一個address屬性字符串設置爲監聽和響應通知,此通知頻道的URL。 這是您的Webhook回調URL,它必須使用HTTPS

我很抱歉,但我不認爲這是一種解決方法。

+0

好的謝謝,我明白了。我只是想知道是否可以將自簽名的https設置爲本地主機,並使用URL作爲webhook指向發佈到watch()的JSON變量。 – Jonathan

+0

看看:https://ngrok.com/ – hybor

+0

好的謝謝你的指針! – Jonathan