How do i share a google spreadsheet with a service account?

How do i share a google spreadsheet with a service account?
When you need to access Google Sheets data from Python script you will need to prove that you have access to the resource. There are several options to authenticate to Google API, one of them is Service Account. We will show you how to get JSON file with credentials to access Google Sheets.

Steps

Creating JSON file with credentials to access Google Sheets API is fast and easy. It can be summarized with the following steps.

  1. Create a new project
  2. Enable Google Drive API and Google Sheets API
  3. Create Service Account credentials
  4. Download JSON file with credentials

The schema showing the concept of how does it work.

How do i share a google spreadsheet with a service account?

Create a new project

Please open your Google Cloud console in your web browser. You can open it by navigating the https://console.cloud.google.com/.

In this example, we will create a new project. It will store all details about our connection to Google Sheets API. In my opinion, it is good to have a separate project for accessing a Sheets, because in the case of emergency it is easy to revoke the access.

If it is your first project then you will see the view similar like in the image below:

How do i share a google spreadsheet with a service account?

If you already have some projects in your console, please click on project in the top menu bar, and you will have dialog window with NEW PROJECT button.

How do i share a google spreadsheet with a service account?

The form for project creation will be displayed. Just set the name and click CREATE. In this example, I’ve used mljar-sheets-project as a name. You can leave Location field empty.

How do i share a google spreadsheet with a service account?

After Project creation, you should see view like in the image below. Please click ENABLE APIS AND SERVICES button to add access to selected APIs.

How do i share a google spreadsheet with a service account?

Enable access to APIs in project

In the next step, we will need to enable access to APIs for our project. The project will need to have access to:

  • Google Drive API,
  • Google Sheets API

Why do we need to give additional access to Google Drive API? It’s because Google Sheets are stored in Google Drive. Changes made on Sheets are also changes on files in the Google Drive.

Let’s search for Google Drive API:

How do i share a google spreadsheet with a service account?

When you found it, just click ENABLE button:

How do i share a google spreadsheet with a service account?

After API is enabled, you will see the below view. Please click on APIs & Service in the left menu.

How do i share a google spreadsheet with a service account?

We need to search one more API. Please enter Google Sheets API in the search box:

How do i share a google spreadsheet with a service account?

Like before, please enable it with ENABLE button:

How do i share a google spreadsheet with a service account?

Create Service Account credentials

After enabling both (Drive and Sheets) APIs you will have the view like in the image below. You can click there CREATE CREDENTIALS button.

How do i share a google spreadsheet with a service account?

The other option for credentials creation is to go to the project dashboard and click CREATE CREDENTIALS button there:

How do i share a google spreadsheet with a service account?

You will have a menu displayed with several options. Please select Service account.

How do i share a google spreadsheet with a service account?

Service Account option will create a new account that we will use only to access Google Sheets API. Google Cloud will create a new email associated with Service Account. We will share selected Spreadsheets with that email. Such solution is good and safe in my opinion. We have an option to set a granular access to the resources that we want to share with our script. In the case of stolen credentials, we simply remove the credentials or even a whole project.

Please fill the Service account name and click CREATE AND CONTINE.

How do i share a google spreadsheet with a service account?

In the next step, set access to the project. In this example, I set the role to Owner. You can set more restrictive role if needed. Click DONE button.

How do i share a google spreadsheet with a service account?

Create JSON file with credentials

After creating the Service Account you will see a view with your credentials. Please click on your Service Account.

How do i share a google spreadsheet with a service account?

You will see its details. Please select KEYS tab.

How do i share a google spreadsheet with a service account?

In the KEYS view, just click ADD KEY and Create new key:

How do i share a google spreadsheet with a service account?

You will see a dialog where you can select the key format. Please select JSON (should be the default) and click CREATE. It should start download of your JSON file with credentials.

How do i share a google spreadsheet with a service account?

Congratulations! The JSON file is ready to use in your Python scripts. Let’s test it.

Authenticate in Python

I will use Python code that is generated with MLJAR Studio notebook:

import gspread
# connect got Google Sheets
gspread_client = gspread.service_account(filename="/path/to/your/credentials.json")
# list all available spreadsheets
spreadsheets = gspread_client.openall()
if spreadsheets:
    print("Available spreadsheets:")
    for spreadsheet in spreadsheets:
        print("Title:", spreadsheet.title, "URL:", spreadsheet.url)
else:
    print("No spreadsheets available")
    print("Please share the spreadsheet with Service Account email")
    print(gspread_client.auth.signer_email)

This is how it looks in the MLJAR Studio:

How do i share a google spreadsheet with a service account?

You should see the message that no spreadsheets are available. You will see also an email that is associated with Service account. You need to share Spreadsheets with that email.

The email can be also checked in Credentials view in Google Cloud Console:

How do i share a google spreadsheet with a service account?

You share the Spreadsheet with Service Account email in the same way as you will share with a friend:

How do i share a google spreadsheet with a service account?

Remove the project

If you would like to remove the project, please click three dots near your profile picture in the top right corner and select Project settings.

How do i share a google spreadsheet with a service account?

There, please click SHUT DOWN to delete the project.

How do i share a google spreadsheet with a service account?

That’all, project is scheduled for removal.

Summary

In this article you have created a JSON file with credentials to authenticate Python code to access Google Sheets data. We use Service Account credentials. To have access to the selected Spreadsheet, it needs to be shared with email associated with Service Account. It is much safer apporach than sharing all your Spreadsheets at once.

We are looking for feedback or comments from you. You can reach us by email to Ola.

We are working on MLJAR Studio desktop application. It is a no-code framework for creating Python scripts. The application is still under the development. If you would like to be informed about the release please fill the form.



💌 Join our newsletter 💌

Subscribe to our newsletter to receive product updates


How do i share a google spreadsheet with a service account?

Share your Python Notebooks with others



Can multiple users work on the same Google Sheet?

Share files in Drive, Docs, Sheets, or Slides Up to 100: You can let up to 100 people with view, edit, or comment permissions work on a Google Docs, Sheets, or Slides file at the same time.

Can individual Google sheets be shared?

Google sheets only allows sharing the whole spreadsheet. If you want to share a single sheet, you either use publish to web or import range as workarounds.

How do I pull data from Google Sheets to API?

Extracting data from Google Sheets via API.
Configure the Google Cloud Platform and enable the Google Sheets API..
Generate and retrieve the API key..
Allow your sheet to be accessible via API key and retrieve the Spreadsheet ID and Range parameter..
Build and format the API request in Query Builder..

How do I enable API in Google Sheets?

Enable an API.
Go to the API Console..
From the projects list, select a project or create a new one..
If the APIs & services page isn't already open, open the console left side menu and select APIs & services, and then select Library..
Click the API you want to enable. ... .
Click ENABLE..