Reputation: 69
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?
Upvotes: 6
Views: 19477
Reputation: 409
This is how it works for me, read for example documents from google sheet
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',
'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('my-drive.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("contactos-xxx").sheet1
# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)
Upvotes: 0
Reputation: 41
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
Upvotes: 3
Reputation: 13572
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.
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.
Upvotes: 7
Reputation: 905
Do not forget to allow access to the sheet for the email (client_email) that stated in credentials.json, that you downloaded from API dashboard:
{
"type": "service_account",
"project_id": "XXXXXXXXXXX",
"private_key_id": ".........",
"private_key": .........
"client_email": "[email protected]",
"client_id": "..............",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
.......
}
I mean, you should go to your Google Sheet file and manually allow access for this email there. That worked fine in my case.
Upvotes: 1