如何从Python验证AWS Lambda中的Google API

我有一些简单的Python代码作为REST服务运行,用于自动创建Google日历条目。代码的auth部分如下所示:

    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    calendar_service = build('calendar', 'v3', credentials=creds)

我正在尝试将其移动到AWS Lambda函数。作为Lambda函数运行的相同代码会导致以下错误。

[ERROR] OSError: [Errno 30] Read-only file system: 'token.json'
Traceback (most recent call last):
  File "/function/app-google-calendar.py", line 54, in handler
    insert_result = calendar_service.events().insert(calendarId=MENU_CALENDAR_ID, body=new_event).execute()
  File "/function/googleapiclient/_helpers.py", line 131, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/function/googleapiclient/http.py", line 922, in execute
    resp, content = _retry_request(
  File "/function/googleapiclient/http.py", line 190, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "/function/oauth2client/transport.py", line 186, in new_request
    credentials._refresh(orig_request_method)
  File "/function/oauth2client/client.py", line 761, in _refresh
    self._do_refresh_request(http)
  File "/function/oauth2client/client.py", line 802, in _do_refresh_request
    self.store.locked_put(self)
  File "/function/oauth2client/file.py", line 85, in locked_put
    f = open(self._filename, 'w')

根本原因似乎很清楚。也就是说,Lambda文件系统是只读的。但是,我一直找不到文档或示例来说明如何在不需要文件系统写访问的情况下完成此oauth舞蹈。

转载请注明出处:http://www.wxmcsj.com/article/20230331/1885303.html