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.
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..
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.
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.
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
First up is a discussion on Using OAUTH2 with a webapp published 'as me'
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
This uses JSON web tokens and is not actually OAuth2, but goa can also be used as described in Firebase custom authentication with goa.
For more like this, see Google Apps Scripts snippets. Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.
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'Reilly, Amazon and all good bookshops. You can also read a preview on O'Reilly
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > OAuth2 for Apps Script in a few lines of code >