Creating Service Accounts for programmatic access to Google Drive APIs

Google Drive is one of those cloud technologies that democratized cloud access to data storage. It lets you securely push all kinds of data into and out of the cloud via Google-provided APIs. APIs and documents are bound to permissions, roles, and identities. Programs accessing Google Docs require credentials, preferably least-privilege credentials, which exist just for a single program's needs. Google IAM supports Service Accounts that are not tied to any human.  They can be enabled and disabled without impacting individual users. 

Accessing Google Docs via API means you have to enable Google Drive API in a project, create an identity/credentials for the program, and then give the identity access to the docs or the API.  There are plenty of good tutorials that walk you through setting up an account. They are often light on the overall process or how the steps tie together.  It can be confusing the first time or 10 you go through it.

  1. Google services are enabled on a per-project basis. This lets you isolate access and capabilities through judicious project management. Projects are sometimes referred to as domains in the user interface.
  2. Credentials like Service Accounts, API Keys, or OAuth IDs exist within the scope of a project sometimes referred to as a domain. Credentials/Identities can exist in the root domain or any lower-level domain.  Each of these identity types has their own advantages and disadvantages.  I mention Service Accounts here because Pysheet and other Python libraries support JSON-based Service Account keys as identity credentials. 
  3. Keys provide a way of authenticating as a Service Account type of Identity when making a request.  Google supports JSON and legacy P12 key formats. Libraries can associate these tokens with requests made to Google APIs.
Those three tasks flow as follows.  Start with the Google API Console or the Google Cloud Resource Manager  I recorded a video explaining this diagram.



All that is left to do is to grant the Service Account Access to your Google Drive Document.  A short explanation is in the section below.

Domain/API/AIM/Account/Document Relationships

I started with the step-by-step because people want to get stuff done. Now we can look at how it all comes together.

The flow described above creates a Service Account that has a role that has access to APIs that were enabled within a project. The flow creates a JSON key that can be presented by a program to the APIs to authenticate as the Service Account. That key identifies the Service Account and is validated as both having access to APIs and having read-only or read-write permissions to a specific document.


Detailed Walkthrough


A detailed, screen-by-screen, walkthrough will have to wait for another article.  I used this setup guide by Erik Rood targeted at Python pygsheets users when I set up my credentials.

Example Domain / Project Hierarchy

This shows a domain with an associated set of projects.  Each project can have its own set of enabled Google services and domain-specific identities.  My exploration project here is My Project 2237

Granting access to an existing Google Drive document


Service Accounts have email addresses like a user account.  This means you can grant access to individual Google Drive documents in the same way you grant to any human user. 













Just add the Service Account email address to the list of users granted view or edit permissions on a document.






Walking through 

Enabling APIs, creating identities, and granting keys

Creating Google Identity and Credentials for Programmatic Access to Google Drive
The moving parts when programs access Google drive APIs

Revision History

Created 2024 02
Thanks to https://www.linkedin.com/in/kenny-freeman-denver/ for working on this with me.

Comments

Popular posts from this blog

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

DNS for Azure Point to Site (P2S) VPN - getting the internal IPs