1
votes

I'm getting an error when trying to update the value of a spreadsheet via an external editor. I'm using the python library 'gspread'; I have read access to the document but when I try to update a cell I get an API error.

Here is the code (security details obscured). I've looked at other permission error solutions on stackoverflow, but they don't apply to the scenario where an app can read data but not write it.

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = [""
      , "https://www.googleapis.com/auth/script.external_request"
      , "https://www.googleapis.com/auth/spreadsheets"
      , "https://www.googleapis.com/auth/script.container.ui"
      ,   'https://spreadsheets.google.com/feeds'
      , "https://mail.google.com/"
      , " https://www.googleapis.com/auth/drive"]

credentials = ServiceAccountCredentials.from_json_keyfile_name('project-id-[hiddenID].json', scope)

gc = gspread.authorize(credentials)

sht2 = gc.open_by_url('https://docs.google.com/spreadsheets/d/[hiddenID]')

worksheet = sht2.get_worksheet(0)

val = worksheet.acell('B1').value
print(val)



worksheet.update_acell('B6', 'Bingo!')

It's the last line that calls the error

APIError: {
  "error": {
    "code": 403,
    "message": "The caller does not have permission",
    "status": "PERMISSION_DENIED"
  }
}

I've tried giving the API owner access, but I guess I'm doing the permissions wrong.

Thanks for any help!

1
Have you changed scopes but not refreshed authorization? (i.e. deleted any local cached credentials)tehhowch
when you say local cached credential, would that be local files, like credentials.json? I deleted every other json file which had credentials and it didnt work. Am i taking your suggestion correctly?tom
The file that stores your client ID and secret (or equivalent) does not need to be deleted--the file that stores an access or refresh token does. The tokens are not valid for new or different scope authorizations. Also, make sure you have shared the document with the service account.tehhowch

1 Answers

2
votes

This was solved by adding the same email on the account as an editor on the document. Simple fix.