我在运行应列出我的Google日历事件的Python 3.7 Google Cloud Function时遇到了一些麻烦。我正在使用Google Calendar 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 Calendar APP中与服务帐户电子邮件共享了日历。
该功能已部署好,但是在测试时发现以下错误:
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)
希望对您有帮助!