<aside> ⚠️ DEPRECATED!!! This guide migrated to our docs. Please view the current version at: https://docs.streamlit.io/en/stable/tutorial/private_gsheet.html

</aside>

Introduction

This guide explains how to securely access a private Google Sheet from Streamlit sharing or Streamlit for Teams. It uses the gsheetsdb library and Streamlit's secrets management (requires streamlit v0.80.0 or higher).

<aside> ☝ If you are fine with enabling link sharing for your Google Sheet (i.e. everyone with the link can view it), the guide Connecting Streamlit to a public Google Sheet shows a way simpler method of doing this. If your Sheet contains sensitive information and you cannot enable link sharing, keep on reading.

</aside>

Step 1: Create a Google Sheet

<aside> 🏃 If you already have a Sheet that you want to access, feel free to skip this step.

</aside>

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/56b5d247-0625-43c0-86a7-c88876bdbc09/Screenshot_2021-05-07_at_20.22.18.png

Step 2: Enable the Sheets API

Programmatic access to Google Sheets is controlled through Google Cloud Platform. Create an account or sign in and head over to the APIs & Services dashboard (select or create a project if asked). As shown below, search for the Sheets API and enable it (double click on images to enlarge):

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/bce97cc7-64b0-425a-9936-271d6c8afc62/Screenshot_2021-05-07_at_23.23.48.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/bd8155fc-9d39-4249-9ff3-7dc18ed38624/Screenshot_2021-05-07_at_23.24.36.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/29c8bd2c-3bd7-434c-be9a-b65c95da73e4/Screenshot_2021-05-07_at_23.25.04.png

Step 3: Create a service account & key file

To use the Sheets API from the Streamlit Cloud, you need a Google Cloud Platform service account (a special account type for programmatic data access). Go to the Service Accounts page and create an account with the Viewer permission (this will let the account access data but not change it):

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/75a11e1e-4e66-4e4b-b1e5-514bfbae7f66/Screenshot_2021-04-13_at_20.25.23.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d37b9fd6-c367-4e95-9b65-547e636bb6a8/Screenshot_2021-04-13_at_20.26.38.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/454d58a2-3470-428b-8c36-ce41e96cdafc/Screenshot_2021-04-13_at_20.26.55.png

<aside> ☝ If the button CREATE SERVICE ACCOUNT is gray, you don't have the correct permissions. Ask the admin of your Google Cloud project for help.

</aside>

After clicking DONE, you should be back on the service accounts overview. First, note down the email address of the account you just created (important for next step!). Then, create a JSON key file for the new account and download it:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/1d82958b-5499-4e58-944a-e7650bda4fb3/Screenshot_2021-04-13_at_20.28.43.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6a02c5ac-a928-4d19-9339-1e653bdb03d0/Screenshot_2021-04-13_at_20.29.54.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/778e5387-c989-49e8-9aa2-1336f4f8ef6e/Screenshot_2021-04-13_at_20.30.44.png

<aside> 🤫 Store the key file in a safe location and don't share it with anyone.

</aside>

Step 4: Share the Google Sheet with the service account