5
votes

I'm following the tutorial from this official link : https://developers.google.com/sheets/quickstart/python

I did execute 'quickstart.py' to authenticated. After that, I ran 'quickstart.py' again and saw the data from 'https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit#gid=0' as this tutorial gets.

I did change spreadsheet ID to my own id and make it to get the data from my spreadsheet by the method :service.spreadsheets().values().get().execute()

But my goal is to add data to my spreadsheet, so I used the method 'update' as below:

rangeName = 'A2:D'
body['range'] = rangeName
body['majorDimension'] = 'ROWS'
body['values'] = ['test','test','test','test']
result = service.spreadsheets().values().update(
    spreadsheetId=spreadsheetId, range=rangeName, body=body).execute()
print('result:'+str(result))

Then I got an error :

googleapiclient.errors.HttpError: https://sheets.googleapis.com/v4/spreadsheets/MY_SPREADSHEET_ID/values/A2%3AD?alt=json returned "Request had insufficient authentication scopes.">

I don't know why this erorr occurs when trying to update my sheet and why this error doesn't occur when trying to get data from my sheet.(If it is cause by authentication, the method 'get' should cause it too!)

Thank you.

1
Please see this link - Eugene
@Eugene Thank you for your link, but of course, I already found that post and it's not a 'gcloud' but 'google spreadsheet api' with python - LKM

1 Answers

6
votes

The quickstart.py example sets the scope to:

https://www.googleapis.com/auth/spreadsheets.readonly

To update the spreadsheet you need to set the scope to:

https://www.googleapis.com/auth/spreadsheets

You can do this by first deleting the existing authentication file in ~/.credentials (that is the location on a raspberry.). It will likely be called "sheets.googleapis.com-python-quickstart.json.

After you removed it you will need to re-authenticate, which should happen automatically when you re-run the script.