Reputation: 23
I'm having some trouble while running a Python 3.7 Google Cloud Function that is supposed to list the events of my Google Calendar. I'm using the Google Calendar API.
This is my code.
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'])
I'm using the App Engine service account as the function identity and its key is the json credential.json that I reference in credentials variable above. This json file is packed in the function bundle at its root (image attached). Additionally, I've shared my calendar with the service account e-mail, directly inside the Google Calendar APP.
The function is deployed OK, but when testing it I find the following errors:
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
Upvotes: 0
Views: 717
Reputation: 15357
The cache_discovery=False
flag need to be declared when building your service.
Under the assumption you have google.appengine installed using
pip install googleappengine
You need to change:
service = build('calendar', 'v3', credentials=credentials)
to:
service = build('calendar', 'v3', credentials=credentials, cache_discovery=False)
Upvotes: 1