2

Many of my partners at work are comfortable mainly with spreadsheets, and not python, scala, java, SQL, etc. These people are non-technical, but they need data and it's my job to get it in their hands. Reading about Google colaboratory, and their example notebook on io, I discovered gspread and the apparent ease of authentication:

from google.colab import auth
auth.authenticate_user()

import gspread
from oauth2client.client import GoogleCredentials

gc = gspread.authorize(GoogleCredentials.get_application_default())

We use a local deployment of Jupyter with several customizations for simplifying access to Presto, Hive, Spark, etc, so I tried installing colab in our environment to no avail (it's not open source). The next best approach I found was using oauth2, but this seems to require a GCP account for access to the Google Developer's Console. This seems overly complicated for something that boils down to authenticating with a remote service.

So, group mind, how do you most easily authenticate with gspread in a jupyter notebook?

1 Answers1

2

I can explain the ingredients that make this work in Colab:

  • the call to authenticate_user invokes gcloud auth login --enable-gdrive-access (with other args).
  • We then set the GOOGLE_APPLICATION_CREDENTIALS environment variable to an exported copy of those credentials.
  • Any code using the Application Default Credentials can now discover those credentials; in particular, oauth2client finds them (as in the sample above).

As always, you should be careful about where you put credentials; in the case of Colab, we have a fairly controlled system, and are OK with this setup (though as with all things, it could change in the future).

Craig Citro
  • 6,505
  • 1
  • 31
  • 28