Ahh, user rights. The cause and solution to all of life’s identification problems. In this blog post I explain how you can access (private) google spreadsheets using the Python gspread library. Before you get started: make sure you have administrator rights to the spreadsheets you are trying to work with.
Let’s get started:
- Create a Google Cloud Platform project.
- Enable the Sheets API
- Go to the service account page, select your project and create a new service account.
- Press ‘Continue’ when you reach step 2 if you don’t want to give this service account access to the GCP project
- In step 3, click Create Key and select JSON. Download the key.
- Once downloaded, rename the file to cred.json and move it to your project folder.
- Use the following code authorize and load in the spreadsheets. Please, fill in your spreadsheet URL. Don’t run the code just yet. Else, you’ll run into the ‘The caller does not have permission’ (403) error.
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import numpy as np
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('cred.json', scope)
gc = gspread.authorize(credentials)
gsheet = gc.open_by_url('<YOUR URL HERE>')
spreadsheets = [spreadsheet.get_all_values() for spreadsheet in gsheet.worksheets()]
headers = [data.pop(0) for data in spreadsheets]
data = [pd.DataFrame(spreadsheets[i], columns = headers[i]) for i in range(0,len(spreadsheets))]
- Go back to the service account page and copy the email address associated with your service account.
- Go to your spreadsheet and click Share in the top right corner.
- Click Advanced in the bottom right corner of the window that pops up.
- Send the invite to the service account by pasting the email address and clicking Send.
If you wait for a minute now and run the Python code, you will load in all the data inside your spreadsheets inside a list of pandas DataFrames.
Great success!
Hi Roel! 🙂
Just wanted to say thank you for this article, you saved me from going crazy!
My spreadsheet is part of a Google Apps organization, but even after adding it in the Google Apps Admin panel as an API client under Admin > Security > Advance settings > Domain-wide delegation, it wasn’t working…
The “Share” section in the spreadsheet itself + adding the Service Account email totally did it!
You’re welcome!
Thank you for your helpful advice. Works!
You’re welcome!
thank you so much. i didnt know I had to share the spreadsheet with the email in the credential
Pingback: The caller does not have permission перевод — Учим английский вместе
My partner and I stumbled over here different page and thought
I should check things out. I like what I see so now i’m following you.
Look forward to looking into your web page repeatedly.
Every weekend i used to go to see this web page, because i
wish for enjoyment, as this this website conations genuinely good funny information too.
What’s up to all, the contents existing at this site are genuinely remarkable for people experience, well,
keep up the good work fellows.
Piece of writing writing is also a fun, if you be acquainted
with afterward you can write otherwise it is complex to write.
Whats up are using WordPress for your site platform?
I’m new to the blog world but I’m trying to get started and set up my
own. Do you require any html coding expertise to make your own blog?
Any help would be really appreciated!
Hi there, everything is going sound here and ofcourse every one is sharing data,
that’s in fact fine, keep up writing.
Very rapidly this web page will be famous among all blogging
and site-building users, due to it’s nice articles
Way cool! Some extremely valid points! I appreciate you writing this
write-up plus the rest of the site is also very good.
Link exchange is nothing else except it is only placing
the other person’s webpage link on your page at proper place and other person will also
do similar for you.
It’s genuinely very complex in this busy life to listen news on Television, therefore I simply use world wide web
for that reason, and take the latest information.
This text is worth everyone’s attention. How can I find out more?
Thank you for the good writeup. It actually was a leisure account it.
Glance complicated to more brought agreeable from you!
By the way, how can we keep in touch?
I believe this is one of the most important information for me.
And i am satisfied studying your article. However wanna observation on some normal issues, The site taste is wonderful, the articles is in reality
great : D. Excellent task, cheers
This piece of writing provides clear idea in favor of the
new visitors of blogging, that in fact how to do running a blog.