Some OAUTH2 walkthroughs

This section contains some different methods of getting an access token for use with Google APIS that are not built in or advanced services. 

All the demo code for the full app used in these examples is available on github. Note that I'm using the same code for all these examples, so the final source on github will have evolved in some details from the writeup here.

Quick summary of all OAuth2 scenarios


Using the apps script token

The first example is the simplest, where we reuse the Apps Script token. Here's an example using the Native Drive API, but borrowing an access token from Apps Script..

Using Goa

For the next examples, we'll need the cGoa library which  is available under this project key, or on github. All the tests described for this topic are also available on github.

MZx5DzNPsYjVyZaR67xXJQai_d-phDA33

There is a tutorial and complete documentation on Goa available here.



Using a service account

The next simplest method is to use a service account. This is most suitable if you are accessing resources associated with a project as opposed to an individual. Here's an example using the Cloud Vision API with a service account.

Using a regular OAUTH2 flow

This is the most complex, but the Goa library actually makes it fairly straightforward too. You should decide whether to use the UserProperties or ScriptProperties based on whether the application is using your resources (running as you), or who ever is using its resources. For more information on that, see Accessing as the user running the script


Next, Using OAuth2 when published as 'user accessing the webapp' enhances the example by adding Google Picker to be able to look at a user's Drive files.

Finally here's a completed app with all kinds of scenarios built in. Comparing Google and Microsoft - image emotion detection

Using Goa for Firebase custom authentication

This uses JSON web tokens and is not actually OAuth2, but goa can also be used as described in Firebase custom authentication with goa.

Goa Tutorial slides

Goa tutorial


For more like this, see Google Apps Scripts snippets. Why not join our forumfollow the blog or follow me on twitter to ensure you get updates when they are available. 

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, All formats are available from O'ReillyAmazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.