Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google API (Sheets) API Error code 403. Insufficient Permission: Request had insufficient authentication scopes

I am trying out a project where I am able to use python (im using jupyter notebooks on Anaconda) to read data from google sheets. I watched a few videos and guides and replicated the code. However, I am unable to get the code to work correctly

import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('test.json',scope)
client = gspread.authorize(creds)
data = gc.open('TEST').sheet1
print(data.get_all_records())

The error message I got was

APIError: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission: Request had insufficient authentication scopes."
   }
  ],
  "code": 403,
  "message": "Insufficient Permission: Request had insufficient authentication scopes."
 }
}

Any advice on what I should do?

like image 971
hotdogexplosion Avatar asked Jan 27 '23 03:01

hotdogexplosion


2 Answers

Authentication scopes allow your application to perform certain actions on the service using OAuth2 authentication. When using google APIs, please refer to the Google API scopes sheet.

See the Google Sheets API V4 related scopes in the below image.Please make sure not to give extra permissions which could affect the security of your application.

enter image description here

Please Note: Since you are planning on changing the scopes, first delete the token.json file created in the local project folder, and then allow access to your application again. This will create a new token.json file.

like image 177
Keet Sugathadasa Avatar answered Jan 28 '23 15:01

Keet Sugathadasa


i saw a new way on github using a google lib. Doing this

import gspread
from google.oauth2.service_account import Credentials

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

creds = Credentials.from_service_account_file('client_secret.json', scopes=scope)

client = gspread.authorize(creds) 

i used this link: https://github.com/burnash/gspread

like image 40
Gabriel Guimarães Avatar answered Jan 28 '23 15:01

Gabriel Guimarães