14

Using developers.google.com we created api user and downloaded credentials as json file. Now On my macbook gspread authentication is working fine while using credentials.json. when moved same config to linux server on aws its giving 403 insufficient permission error.

Pip and python version are same.

exception

gspread.v4.exceptions.APIError: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}

basic code

import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
client = gspread.authorize(creds)

sheet = client.open('MySheetName').sheet1
Ahsan Naseem
  • 1,046
  • 1
  • 19
  • 38
  • I am seeing a similar problem on one of my two linux servers. The only difference I can currently between my servers is what version of gspread they have installed, maybe check what version of gspread your two machines are using? Please let me know how you resolve! – Weir_Doe Mar 14 '18 at 01:30
  • I rolled back the version of gspread on my linux server which was experiencing the same issue and this seems to have fixed the issue for me, implying gspread 2.0 might be to blame, hope yours is a similar issue. This is what I ran: pip install 'gspread==0.6.2' --force-reinstall – Weir_Doe Mar 14 '18 at 01:59

3 Answers3

42

Try to change your scope variable to the following:

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']

Make sure Drive API is enabled in API console.

gspread has been upgraded and it's now based on API v4. It's faster but it requires updates in scope.

Here's the same issue: https://github.com/burnash/gspread/issues/512

Burnash
  • 3,181
  • 2
  • 31
  • 35
  • 1
    worked for me! at first it gave me 'permission denied' and a link to activate first execution. after that it worked. – Vincenzooo Apr 28 '18 at 18:02
2

You appear to be using a service account. In order for the service account to access your sperad sheet it needs to have access to it.

Make sure that you share the sheet with the service account email address you can do that though google drive web page

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I don't think this can this be the issue, OP says the service account works on a Macbook but not on the linux server, so it must have access. – Weir_Doe Mar 14 '18 at 01:38
0

IF you are unable to access even after updating the scope, try running this command in your terminal:

sudo pip install 'gspread==0.6.2' --force-reinstall

Yogamurthy
  • 988
  • 15
  • 22
  • 1
    I would discourage people from downgrading: gspread 0.6.2 uses the older version of Sheets API (v3). Google strongly recommends upgrading from v3 as it will be deprecated. See https://developers.google.com/sheets/api/v3/ – Burnash May 11 '18 at 10:08