Back

Read a Google Doc Using a Service account (Without Oauth 2 consent)

• 22.03.20 •

Tom Larkworthy • Senior Cloud Architect

Here is code to read a Google doc without Oauth 2 consent steps, written in Typescript. This allows you to experiment with the Google Doc's API on work documents without involving your Domain administrator. I could not find an easy-to-follow example of this, so I created this one.

First you need to create a Google Cloud Project, enable the Google Doc API, and create a Service Account.

A service account has a Google Identity with an email which you can "share" docs with as you would with a normal person.

Share a google doc with a service account

To programatically login as a service account from code, you need to furnish a private service account key. You can find the email address the service account is using within. You can use the credentials with the Google API libraries to make requests as the Service Account, including reading a Google Doc file.

Note this file is sensitive like a password and requires careful thought about storage. If you deploy your code into a Google Cloud Function, the auth library can pick up the service account of the executing function and you can avoid dealing with credential files altogether.

Embedded content: https://gist.github.com/tomlarkworthy/de4d2d45474c47b8faac523709017eb3?file=index.ts

Sign up for Futurice news

Futu Connect is our semi-regular roundup of all things Futurice. Be in the know about career-changing events, industry-leading content, non-profit passion projects, and an ever-changing job board.

Enter your email address below.