I am trying to achieve a simple goal: run python (2.7) script to read/update google sheet using Sheets API v4. I followed these steps:
- Created google cloud platform account
- Created service account and downloaded .json file with credentials
- Installed google client library
- Shared the spreadsheet with service account
- Wrote a very primitive script that should read the data from a spreadsheet
I have also read a lot of official documentation on how to connect python script to google sheets. Main articles and guides:
- https://developers.google.com/identity/protocols/oauth2/service-account#python_1
- https://developers.google.com/sheets/api/guides/values#python
- https://developers.google.com/sheets/api/quickstart/python
This is the script I am trying to run:
from googleapiclient.discovery import build
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SERVICE_ACCOUNT_FILE = 'service_account.json'
spreadsheet_id = '1KDCwwoIavfSINhDP9OKmieQux6LxhVC_ESsDQEma_7Y'
range_name = 'A1:A2'
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('sheets', 'v4', credentials=creds)
result = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_name).execute()
rows = result.get('values', [])
print('{0} rows retrieved.'.format(len(rows)))
When I run this script api call is not being made. Script always fails when build function tries to create service object that is needed to make a request. And this is the error log I am getting:
Traceback (most recent call last):
File "google_sheets_update.py", line 15, in <module>
service = build('sheets', 'v4', credentials=creds)
File "/env/lib/python2.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "/env/lib/python2.7/site-packages/googleapiclient/discovery.py", line 286, in build
adc_key_path=adc_key_path,
File "/env/lib/python2.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "/env/lib/python2.7/site-packages/googleapiclient/discovery.py", line 455, in build_from_document
if isinstance(client_options, six.moves.collections_abc.Mapping):
AttributeError: '_MovedItems' object has no attribute 'collections_abc'
I know that attribute error is explicit but I wasn't able to find any resource that will explain why this error occurs here and how to fix it. If someone could point me to what am I doing wrong and how to solve it I will appreciate it.