Google Sheets API在本地工作,但从AWS Lambda运行时连接超时

时间:2019-08-08 06:54:38

标签: python python-3.x aws-lambda google-oauth google-sheets-api

我有一些代码可以在本地完美运行,而根本无法在AWS Lambda上运行。就像API被阻止一样,我不确定下一步要查找什么。

我可以在网络上打其他事情,所以这不是一般的路由问题,而且我从AWS Run中收到套接字超时错误。

我尝试了几种不同的库,包括主库的旧版本。他们每个人都在本地工作,而不是在AWS上工作。

#!/usr/bin/env python3

# replace
creds_file = "/path/to/creds.json"

import pickle
import os.path
from googleapiclient.discovery import build
from google.oauth2 import service_account

scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']

SAMPLE_SPREADSHEET_ID = "<spreadsheetid>"
# Sample Range
SAMPLE_RANGE_NAME = "Sheet1!A1:D"

creds = None

# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.

if os.path.exists('/tmp/token.pickle'):
    with open('token.pickle', 'rb') as token:
        creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        # Customized
        creds = service_account.Credentials.from_service_account_file(creds_file)
        creds_w_scopes = creds.with_scopes(SCOPES)

    # Save the credentials for the next run
    with open('/tmp/token.pickle', 'wb') as token:
        pickle.dump(creds_w_scopes, token)

# Timeout is Here in the Cloud
service = build('sheets', 'v4', credentials=creds_w_scopes)

# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                            range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])

print(values)

我在本地获得了工作表的结果(只是一些样本数据),尽管它挂在此调用上

service = build('sheets', 'v4', credentials=creds)

然后由于socket.timeout错误而超时。

0 个答案:

没有答案