1

I am trying to use python and google sheets together. This is my code:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

#use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

#Find a workbook by name and open the first sheet
#Make sure you use the right name here.
sheet = client.open("Year 9 Camp").sheet1

#Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

However, it keeps coming up with an error:

Traceback (most recent call last):
  File "C:\Users\cswel\OneDrive\Documents\School\13CSC\spreedsheet.py.py", line 12, in <module>
    sheet = client.open("Year 9 Camp").sheet1
  File "C:\Python34\lib\site-packages\gspread\v4\client.py", line 103, in open
    self.list_spreadsheet_files()
  File "C:\Python34\lib\site-packages\gspread\v4\client.py", line 80, in list_spreadsheet_files
    r = self.request('get', url)
  File "C:\Python34\lib\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"
 }
}

Can someone please help my error or give me a very easy way to connect google sheets and python? I am finding the tutorials on YouTube and online to be difficult to understand.

Much appreciated for any answers given.

M. Freberg
  • 39
  • 1
  • 7
  • This looks like a duplicate of [this question](https://stackoverflow.com/questions/49258566/gspread-authentication-throwing-insufficient-permission). Please look at the answer: https://stackoverflow.com/a/49295205/318359 – Burnash Mar 27 '18 at 08:40

1 Answers1

1

scope = ['https://spreadsheets.google.com/feeds']

just add the drive to the scope like below and it will work worked for me

scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
Andrew
  • 26,706
  • 9
  • 85
  • 101