Recently, I came across a situation where I needed to access a google sheet programmatically. I was pretty happy since I have done this before using my personal account. I also have an article written on it. Check it out here.
So during the process when I shared the client_email to the google sheet, I was not able to share it. The company policy does not allow sharing the sheet outside of the domain. After some research, I was able to sort this out.
Basically what we try achieving here is called domain-wide delegation of authority. You can refer to the official documentation to know more about this.
These are steps that I followed,
1. created a service account and downloaded the JSON file.
2. enable Domain-wide delegation and copy the client id.
3. add the client id and the required scope in the admin console.
scopes are https://spreadsheets.google.com/feeds, https://www.googleapis.com/auth/spreadsheets, https://www.googleapis.com/auth/drive.file, https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar, https://www.googleapis.com/auth/admin.directory.user, https://www.googleapis.com/auth/admin.directory.group
For accessing the google sheet via python I referred to this documentation.
You can also find the code on my Github repo.
import gspread from google.oauth2 import service_account from pprint import pprint from datetime import datetime SCOPES = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"] SERVICE_ACCOUNT_FILE = "creds.json" credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES) delegated_credentials = credentials.with_subject('email@domain_name.com') client = gspread.authorize(delegated_credentials) slots_sheet = client.open("sheet name") slots_sheet = slots_sheet.worksheet("Sheet1") # get all records data = slots_sheet.get_all_records() print(data)