3

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?

3
  • I am assuming you packaged and upload your Lambda function with the packages included in the .zip? Commented Sep 1, 2018 at 22:05
  • @NicholasMartinez yes thats correct. Commented Sep 4, 2018 at 17:28
  • Any updates on this issue @Tezro Solutions? I'm facing same issue. Commented Mar 17, 2019 at 23:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.