Использование Google Calendar API с Python 3.7 облачной функцией Google - PullRequest
0 голосов
/ 17 марта 2020

У меня проблемы с запуском Python 3.7 Облачной функции Google, которая должна отображать события моего Календаря Google. Я использую 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 учетные данные. json, на которые я ссылаюсь в переменной учетных данных выше. Этот json файл упакован в комплект функций по адресу root (изображение прилагается). Кроме того, я поделился своим календарем с электронной почтой учетной записи службы, прямо в приложении Календаря 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

Любые идеи будут очень признательны.

Приветствия,

Гиль

1 Ответ

0 голосов
/ 17 марта 2020

Ответ:

При создании службы необходимо указать флаг cache_discovery=False.

Исправить:

В предположении, что вы имеете google.appengine установлен с помощью

pip install googleappengine

Вам необходимо изменить:

service = build('calendar', 'v3', credentials=credentials)

на:

service = build('calendar', 'v3', credentials=credentials, cache_discovery=False)

Надеюсь, это полезно для вас!

...