使用 Google 日历 API 和 Python 3.7 Google 云函数
Using Google Calendar API with a Python 3.7 Google Cloud Function
我在 运行 一个 Python 3.7 Google 云函数时遇到了一些问题,它应该列出我的 Google 日历的事件。我正在使用 Google 日历 API。
这是我的代码。
from googleapiclient import discovery
import datetime
from apiclient.discovery import build
import datetime
from google.oauth2 import service_account
def listEvents(request):
credentials = service_account.Credentials.from_service_account_file('credential.json',scopes=['https://www.googleapis.com/auth/calendar.readonly'],)
service = build('calendar', 'v3', credentials=credentials)
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
return('Getting the upcoming 10 events')
events_result = service.events().list(calendarId='primary', timeMin=now, maxResults=10, singleEvents=True, orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
return('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
return(start, event['summary'])
我使用 App Engine 服务帐户作为函数标识,它的键是我在上面的凭据变量中引用的 json credential.json。此 json 文件打包在其根目录下的函数包中(附图片)。此外,我已经直接在 Google 日历应用程序中与服务帐户电子邮件共享了我的日历。
功能部署正常,但测试时发现如下错误:
2020-03-17 12:10:51.851 GMTfunction-1wdeeo5kwk4p0 Function execution started
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 Traceback (most recent call last):
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 File "/env/local/lib/python3.7/site-packages/google_api_python_client-1.7.11-py3.7.egg/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 from google.appengine.api import memcache
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 ModuleNotFoundError: No module named 'google.appengine'
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 During handling of the above exception, another exception occurred:
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 Traceback (most recent call last):
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 File "/env/local/lib/python3.7/site-packages/google_api_python_client-1.7.11-py3.7.egg/googleapiclient/discovery_cache/file_cache.py", line 33, in <module>
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 from oauth2client.contrib.locked_file import LockedFile
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 ModuleNotFoundError: No module named 'oauth2client'
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 During handling of the above exception, another exception occurred:
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0
答案:
构建服务时需要声明 cache_discovery=False
标志。
修复:
假设您 google.appengine 使用
安装
pip install googleappengine
您需要更改:
service = build('calendar', 'v3', credentials=credentials)
至:
service = build('calendar', 'v3', credentials=credentials, cache_discovery=False)
我在 运行 一个 Python 3.7 Google 云函数时遇到了一些问题,它应该列出我的 Google 日历的事件。我正在使用 Google 日历 API。
这是我的代码。
from googleapiclient import discovery
import datetime
from apiclient.discovery import build
import datetime
from google.oauth2 import service_account
def listEvents(request):
credentials = service_account.Credentials.from_service_account_file('credential.json',scopes=['https://www.googleapis.com/auth/calendar.readonly'],)
service = build('calendar', 'v3', credentials=credentials)
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
return('Getting the upcoming 10 events')
events_result = service.events().list(calendarId='primary', timeMin=now, maxResults=10, singleEvents=True, orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
return('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
return(start, event['summary'])
我使用 App Engine 服务帐户作为函数标识,它的键是我在上面的凭据变量中引用的 json credential.json。此 json 文件打包在其根目录下的函数包中(附图片)。此外,我已经直接在 Google 日历应用程序中与服务帐户电子邮件共享了我的日历。
功能部署正常,但测试时发现如下错误:
2020-03-17 12:10:51.851 GMTfunction-1wdeeo5kwk4p0 Function execution started
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 Traceback (most recent call last):
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 File "/env/local/lib/python3.7/site-packages/google_api_python_client-1.7.11-py3.7.egg/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 from google.appengine.api import memcache
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 ModuleNotFoundError: No module named 'google.appengine'
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 During handling of the above exception, another exception occurred:
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 Traceback (most recent call last):
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 File "/env/local/lib/python3.7/site-packages/google_api_python_client-1.7.11-py3.7.egg/googleapiclient/discovery_cache/file_cache.py", line 33, in <module>
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 from oauth2client.contrib.locked_file import LockedFile
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 ModuleNotFoundError: No module named 'oauth2client'
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0 During handling of the above exception, another exception occurred:
2020-03-17 12:10:51.915 GMTfunction-1wdeeo5kwk4p0
答案:
构建服务时需要声明 cache_discovery=False
标志。
修复:
假设您 google.appengine 使用
安装pip install googleappengine
您需要更改:
service = build('calendar', 'v3', credentials=credentials)
至:
service = build('calendar', 'v3', credentials=credentials, cache_discovery=False)