Access google sheets for private domains using domain-wide delegation of authority

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,,,,,,

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 = ["",'',"",""]

credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
delegated_credentials = credentials.with_subject('')
client = gspread.authorize(delegated_credentials)

slots_sheet ="sheet name")
slots_sheet = slots_sheet.worksheet("Sheet1")

# get all records
data = slots_sheet.get_all_records()

Happy Programming!!

Leave a Comment