Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gspread.v4.exceptions.APIError - Insufficient Permission #512

Closed
kostykoff opened this issue Mar 13, 2018 · 19 comments
Closed

gspread.v4.exceptions.APIError - Insufficient Permission #512

kostykoff opened this issue Mar 13, 2018 · 19 comments

Comments

@kostykoff
Copy link

kostykoff commented Mar 13, 2018

Hello.
After the OS was reinstalled, the script stopped working.
gspread 2.0.0
python 2.7.12

code

# -*- coding: utf-8 -*- 
import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('123.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open("123")

error

Traceback (most recent last call last):
  File "test.py", line 11, in <module>
    wks = gc.open ("123")
  File "/usr/local/lib/python2.7/dist-packages/gspread/v4/client.py", line 103, in open
    self.list_spreadsheet_files ()
  File "/usr/local/lib/python2.7/dist-packages/gspread/v4/client.py", line 80, in list_spreadsheet_files
    r = self.request ('get', url)
  File "/usr/local/lib/python2.7/dist-packages/gspread/v4/client.py", line 73, in request
    raise APIError (response)
gspread.v4.exceptions.APIError: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}
@kostykoff
Copy link
Author

kostykoff commented Mar 13, 2018

It only works with open_by_key and open_by_url, which does not work for me.
There was another problem:
worksheet.acell('R6').input_value
I get None (in cell '8)

at workheet.acell('R6').value
I get 8

what happened to the library? Apparently on api v3 everything worked fine, how to roll back?

@burnash
Copy link
Owner

burnash commented Mar 13, 2018

Thanks for reporting this.

It is probably related to the upgrade of gspread (2.0.0) which now uses Sheet API v4 (the newest version). Can you check if you have Sheets API enabled for the creds you're using in https://console.cloud.google.com/ ?

@kostykoff
Copy link
Author

Thanks for the answer.
The Google Sheets API is included in https://console.cloud.google.com/ and there are queries.
But gc.open ("NAME_SHEETS") does not work for me, it works through only open_by_key and open_by_url. + problem with reading formulas, see the previous comment.

@burnash
Copy link
Owner

burnash commented Mar 13, 2018

Interesting, I think we're getting close. Let's first figure out the permission problem and then fix the formulas issue. Can you try to enable not only Sheets API in https://console.cloud.google.com/ but also Drive API? (this is what list_spreadsheet_files relies on)

@kostykoff
Copy link
Author

Drive API also included
1
2

@burnash
Copy link
Owner

burnash commented Mar 13, 2018

Ok, not as close as I expected. What do you see when you call gc.openall()? The same error?
Can you also try to generate a new json keyfile and use it?

@abnd90
Copy link

abnd90 commented Mar 13, 2018

I can confirm the exact same issue. I also tried regenerating service api creds.

Traceback (most recent call last):
  File "./main.py", line 94, in <module>
    main()
  File "./main.py", line 81, in main
    sh = gc.open(GSHEET_NAME)
  File "/home/abhinand/dev/pctogs/venv/lib/python3.6/site-packages/gspread/v4/client.py", line 103, in open
    self.list_spreadsheet_files()
  File "/home/abhinand/dev/pctogs/venv/lib/python3.6/site-packages/gspread/v4/client.py", line 80, in list_spreadsheet_files
    r = self.request('get', url)
  File "/home/abhinand/dev/pctogs/venv/lib/python3.6/site-packages/gspread/v4/client.py", line 73, in request
    raise APIError(response)
gspread.v4.exceptions.APIError: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}

@burnash
Copy link
Owner

burnash commented Mar 13, 2018

Ok, try to change your scope to the following:

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

@kostykoff
Copy link
Author

kostykoff commented Mar 14, 2018

Adding to the scope of the https://www.googleapis.com/auth/drive helped open with the help of open.
But the reading of the formulas still gives None.

@kostykoff
Copy link
Author

temporarily rolled back to gspread == 0.6.2

@burnash
Copy link
Owner

burnash commented Mar 14, 2018

I'm glad we've figured out the issue with permissions. Let's fix the formula issue.

inputValue is deprecated in API v4, so I had to deprecate Cell.input_value in gspread accordingly. Here's the new method to retrieve formulas:

workheet.acell('R6', value_render_option='FORMULA').value

See Sheets API > ValueRenderOption for other possible values.

@soad1982
Copy link

I'm having the same issue insufficientPermissions on Version: 2.0.0 adding the scope didn't solve it, just change the error message to:

argument 1 must be string or buffer, not None

Switching to version 0.6.2 fixes the issue.

The problem happens on an alpine:3.4 container, MAC os works just fine.

@burnash
Copy link
Owner

burnash commented Mar 17, 2018

@soad1982 Please share your code sample and stack traceback

kien-ha added a commit to kien-ha/FEH-Spreadsheet-Organizer that referenced this issue Jun 26, 2018
gspread module has received an update to api v4. This api includes
faster insert_row() speeds, bringing down the spreadsheet_work time from
over a min to less than 5 seconds in inserting data into
spreadsheet[1][2].

Issues can be found here:
[1] burnash/gspread#378
[2] burnash/gspread#512

Signed-off-by: Kien Ha <kienha9922@gmail.com>
@Elextric
Copy link

Elextric commented Jul 12, 2018

Excellent I had the same problem.

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

As per your instruction!
Solved it, maybe the scope requirement should be added to the main readme file for the project

@Sur2801
Copy link

Sur2801 commented Feb 10, 2019

@burnash
Dear Sir,
Thanks a ton. for solving such a terrible "raise APIError(response)" error.

@petelah
Copy link

petelah commented Mar 13, 2019

Adding the extra scope worked for me.
Also make sure you have sheets and drive api enabled.

@nibivid
Copy link

nibivid commented Aug 3, 2019

Ha guys i see some of you have some issue with the permission . Just remember that when you enable the google drive api also enable google sheet api as well. It didnt work for me until i enable both the APIs

@QuiTran86
Copy link

I get the same troubles when I try to insert big data into google sheet, I've done this job before, (not enable drive api but it's not raise errror like this) but now, It raise error like this. I try to find out and figure out that I don't enable drive api, I try to do it and now it seems okay!

@NathanVogele
Copy link

I had a similar issue after the update. The trick is to share the sheet with your service worker email. (different than your usual email.) Then it can actually see and edit the file. I forgot to do this and it took me an hour to figure it out. Hope this helps someone else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants