Recently I had to create reports for external partners for a job I had.
“well how hard can it be to create a sheet and share it.”
Turns out, extremely non-accessible to play around and to find relevant material for how to setup the correct permissions or grants for a account.
This tutorial will show a non-technical perspective to setup the correct
credentials and APIs to be able to create a sheet in your drive.
This is for anyone struggling with finding a good resource for how to actually create a sheet programmatically with simple gifs, as they are invaluable.
- create a project in google
- enable APIs for consumption
- setup a service account with correct priviliges
- download the key
- python application to create a sheet and share to anyone in your organization
We need to have a project setup in google to be able to create service accounts and enable apis to be called. This could also be connected to your exisiting project within google of course.
This could also be connected to your exisiting project within google of course!
When we have our project to within our organization, this project enables us to grant access or your admin to the apis for Google Drive and Google Sheets. This essentially starts the apis on googles end to be able to be consumed. For instructions follow the video:
This is the part which is missed in most of the tutorials or guides out there. Even googles own documentation does not give a hint on how the access rights should be granted. What we will do is basically tell google to:
- create a new service account
- a service account (an email and a user within the platform with extra priviliges) exist with access rights
- the service account is granted access over the g-suite instance (meaning sharing, creating, deleting)
Be wary that this service account has now been granted access across the organizations documents. Proceed with caution of how to deal with the authentication of this user. Next we will create a key to be able to verify that we are the service account user.
Create the key and download the key. Please again be wary and store this key in an approriate place like 1password and do not version control it. Best way is to encrypt the file with GPG.
The key is going to look something like this:
Now for the final touch on how to create a google sheet with your newly created account and the credentials.
import gspread from oauth2client.service_account import ServiceAccountCredentials scope = [ "https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive", ] credentials_file = "path_to_credentials.json" creds = ServiceAccountCredentials.from_json_keyfile_name(creds_json, scope) client = gspread.authorize(creds) sheet = client.create("tutorial") # create sheet.share('email@example.com', perm_type='user', role='writer') # share to user of choice
We have now setup the way in which we can programmatically call on google sheet api to be able to create, share, delete documents within your organization. I understand why Google does not want it to be easily accessible to be able to perform these kind of operations, so be wary.
EDIT: one thing that could be done is to place another organization within the organization that the service account can access freely to not be able to see/delete/create any valuable documents that are within the organization itself.