Google Apps Script is an incredible tool to bring more functionality to your GSuite documents, spreadsheets, or presentations. However, its value can be overlooked when you desire to have your document communicate with an external API that uses OAuth2 authentication.

In this post we will walk through the steps to take the Google provided OAuth2 Library and customize it for external services that use the Bearer authorization. This should encompass most types of OAuth2 configuration, but if you have something unique or specific feel free to comment or dig into the examples on the GitHub link.

Great! Let’s start.


1. Include the script in your project

The Google OAuth2 Script is already published as an Apps Script. This makes it easier to include within your script. Simply open your Script Editor then:

  1. Click on the menu item “Resources > Libraries…”
  2. In the “Find a Library” text box, enter the script ID 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF and click the “Select” button.
  3. Pick the latest version.
  4. Click the “Save” button.

2. Register Your Application

Typically an application that requires OAuth2 will also require that you register with that application to obtain the Client ID & secret. On this registration screen typically you’ll be asked to provide a redirect URL. For Apps Script that URL will be:

https://script.google.com/macros/d/{SCRIPT ID}/usercallback

This can be easily found within your script by navigating to File > Project Properties and copying the ‘Script ID’

Image for post

Project Properties within Google Apps Script


3. Creating the OAuth2 Service in your script

For creating this script let’s create a new script file to store this in. File > New > Script File. I create a new file labeled OAuth2 for code management simplicity.

Once we have that script we will need to configure the service. Let’s start by defining our client ID and client secret at the top of our file.

const clientID = '...paste here...';
const clientSecret = '...paste here...';

Now we need to configure out actual service. Paste the below code and modify it based on your API.

function getApiService() {

//This service name below will be used when persisting the service authorized token
   //make sure this is unique within your script.
   return OAuth2.createService('The Service Name Here')
//below is where we are setting our token & auth URLs. Usually the URL will end with '/oauth2/token' or 'oauth2/auth'.
   .setAuthorizationBaseUrl('The service Auth URL')
   .setTokenUrl('The service Token URL')
//We are calling our values from the clientID/clientSecret from above.
   .setClientId(clientID)
   .setClientSecret(clientSecret)
//This is a callback function that we are calling. We will use this in another step.
   .setCallbackFunction('authCallback')
   .setPropertyStore(PropertiesService.getUserProperties())
//Depending on your API below are two common settings. Remove if you don't need. Check out the advanced section of the GitHub link for more options.
   .setScope('read-only')
   .setGrantType('authorization_code'); 
}

#automation #javascript #api #google-apps-script #google #programming

Simple OAuth in Google Apps Script
67.70 GEEK