使用谷歌日历API与Python 3.7谷歌云功能



我在运行Python 3.7谷歌云函数时遇到了一些问题,该函数本应列出我的谷歌日历中的事件。我正在使用谷歌日历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文件被打包在函数包的根目录中(附图片(。此外,我还直接在谷歌日历应用程序中与服务帐户电子邮件共享了我的日历。

该功能部署正常,但在测试时我发现以下错误:

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)

相关内容

  • 没有找到相关文章

最新更新