Google Cloud Platform (GCP) Setup for Gspread
This guide explains how to set up a Google Cloud Platform project, create a
service account, and generate the credentials.json file required for this ETL
project to access Google Sheets.
Overview
To allow our application to read data from a Google Sheet programmatically, we need to:
- Create a Service Account, which is a special type of non-human user account.
- Enable the necessary APIs (Google Sheets and Google Drive).
- Generate a JSON key (
credentials.json) that the application can use to authenticate as the service account. - Share the target Google Sheet with the service account's email address.
Step-by-Step Guide
Step 1: Create or Select a GCP Project
- Go to the Google Cloud Console.
- If you don't have a project, create one. If you do, select the project you want to use from the project dropdown at the top of the page.
Step 2: Enable the Required APIs
You need to enable the Google Sheets API and the Google Drive API for your project.
- Navigate to the API Library.
- Search for "Google Sheets API" and click Enable.
- Search for "Google Drive API" and click Enable.
Step 3: Create a Service Account
- Navigate to the Service Accounts page in the IAM & Admin section.
- Click + CREATE SERVICE ACCOUNT.
- Give the service account a name (e.g.,
g-sheets-etl-runner) and a description. The Service account ID will be generated automatically. Click CREATE AND CONTINUE. - Grant access (Optional but recommended): In the "Grant this service account access to project" step, grant the "Viewer" role. This is not strictly necessary for Gsheets access but is a good practice. Click CONTINUE.
- Grant user access (Optional): You can skip this step. Click DONE.
Step 4: Generate a JSON Key
- You should now be back on the Service Accounts page. Find the service account you just created and click on its email address in the "Email" column.
- Go to the KEYS tab.
- Click ADD KEY -> Create new key.
- Select JSON as the key type and click CREATE.
- A JSON file will be automatically downloaded to your computer. This is
your
credentials.jsonfile.
Step 5: Place the Credentials File
- Rename the downloaded file to
credentials.json. - Move this file into the project root directory. This file is listed in
.gitignore, so it will not be committed to your repository.
Step 6: Share the Google Sheet
This is the final and most important step.
- Open the Google Sheet that you want the application to read.
- Find the service account's email address. You can either copy it from the
"Email" column on the
Service Accounts page
or find it inside your
credentials.jsonfile under the"client_email"key. - In your Google Sheet, click the Share button in the top right corner.
- Paste the service account's email address into the "Add people and groups" field.
- Ensure the service account has at least Viewer permissions.
- Click Send.
Your setup is now complete. The application will be able to use the
credentials.json file to authenticate as the service account and read the data
from the shared Google Sheet.