Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when calling Google Sheets API on Amazon Lamda using Python

I am trying to create a Amazon Lambda python function that uses Google Sheets API to edit a sheet. I am using virtualenv and my script is compatible with python3.6. I have made sure that config.json and token.json are readable. See my code below:

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)
        }

I am getting following error every time I invoke the lambda function:

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

Any ideas what must be going wrong?

like image 671
Tezro Solutions Avatar asked Mar 07 '26 02:03

Tezro Solutions


1 Answers

You can use MemoryCacheinstead:

class MemoryCache(Cache):
    _CACHE = {}

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

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

Then when building service object, you add MemoryCache to cache attribute. So this is your code:

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

New code should be:

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

Hope this help.

like image 77
hqt Avatar answered Mar 08 '26 15:03

hqt