gpt4 book ai didi

python - 使用 Python 在 Amazon Lambda 上调用 Google Sheets API 时出错

转载 作者:行者123 更新时间:2023-11-28 18:08:34 25 4
gpt4 key购买 nike

我正在尝试创建一个使用 Google Sheets API 编辑工作表的 Amazon Lambda python 函数。我正在使用 virtualenv 并且我的脚本与 python3.6 兼容。我确保 config.jsontoken.json 是可读的。请参阅下面的代码:

from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import json

def lamda_handler(event, context):
store = file.Storage("token.json")
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets("credentials.json", 'https://www.googleapis.com/auth/spreadsheets') # If modifying these scopes, delete the file token.json.

creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))

# Call the Sheets API
SPREADSHEET_ID = 'Google_Sheet_ID'
RANGE_NAME = 'Sheet1!A1:A'
result = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME).execute()
value_range_body = {
"majorDimension": "ROWS",
"range": "Sheet1!A1:A",
"values": [
[
"lamda"
],
[
898449
]
]
}

request = service.spreadsheets().values().update(spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME, valueInputOption='RAW', body=value_range_body)
response = request.execute()


values = result.get('values', [])

if not values:
return {
"statusCode": 200,
"body": json.dumps('No data found.')
}
else:
return {
"statusCode": 200,
"body": json.dumps(values)
}

每次调用 lambda 函数时都会出现以下错误:

START RequestId: 43ca08ba-ad6b-11e8-b8e7-251d743c1903 Version: $LATEST
[WARNING] 2018-08-31T22:14:45.3Z 43ca08ba-ad6b-11e8-b8e7-251d743c1903 file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth
Traceback (most recent call last):
File "/var/task/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
from google.appengine.api import memcache
ModuleNotFoundError: No module named 'google.appengine'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/task/googleapiclient/discovery_cache/file_cache.py", line 33, in <module>
from oauth2client.contrib.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.contrib.locked_file'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/task/googleapiclient/discovery_cache/file_cache.py", line 37, in <module>
from oauth2client.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.locked_file'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/var/task/googleapiclient/discovery_cache/__init__.py", line 41, in autodetect
from . import file_cache
File "/var/task/googleapiclient/discovery_cache/file_cache.py", line 41, in <module>
'file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth')
ImportError: file_cache is unavailable when using oauth2client >= 4.0.0 or google-authEND RequestId: 43ca08ba-ad6b-11e8-b8e7-251d743c1903
REPORT RequestId: 43ca08ba-ad6b-11e8-b8e7-251d743c1903 Duration: 3003.24 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 44 MB
2018-08-31T22:14:47.989Z 43ca08ba-ad6b-11e8-b8e7-251d743c1903 Task timed out after 3.00 seconds

知道哪里出了问题吗?

最佳答案

您可以改用MemoryCache:

class MemoryCache(Cache):
_CACHE = {}

def get(self, url):
return MemoryCache._CACHE.get(url)

def set(self, url, content):
MemoryCache._CACHE[url] = content

然后在构建服务对象时,将 MemoryCache 添加到 cache 属性。所以这是你的代码:

 service = build('sheets', 'v4', http=creds.authorize(Http()))

新代码应该是:

 service = build('sheets', 'v4', http=creds.authorize(Http()), , cache=MemoryCache())

希望这对您有所帮助。

关于python - 使用 Python 在 Amazon Lambda 上调用 Google Sheets API 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52123401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com