I created a Google Sheets document, as shown below. In this sheet, I put the value 'This is a test' in A1 cell
To extract data from this Google Sheets file, it is necessary to enable the Google Drive API and Google Sheets API in the Google Cloud Console for the project associated with the account that created the spreadsheet. Additionally, a service account must be created in the same Google Cloud project.
To enable the APIs, go to the Google Cloud Console, navigate to APIs & Services, and search for the Google Drive API and Google Sheets API, then enable both.
For the service account, create a service account in the same project where the APIs were enabled. This service account will be used to authenticate and access the Google Sheets data.
Next, click on your service account and create a JSON key file containing the service account credentials. This file includes all the information required for authentication and is used by the gspread library to connect to Google Sheets.
Now, store this file as an environment variable. This is very important for security, as the file contains sensitive information. Additionally, make sure to add the file to your .gitignore to prevent it from being committed to a public repository. And here is the code that returns the value of the A1 cell.