Running things in parallel that need oAUTH2 - datastore

If you are using Parallel process orchestration with HtmlService along with Database abstraction with google apps script you'll need to know how to work with access tokens for oAuth2. Assuming you've used EzyOauth2 patterns to set up your application to be able to refresh tokens automatically, it's fairly straightforward but you'll need to be familiar with EzyOauth2 patterns and Datastore driver first.

The profile

First let's look at the async profile. It's no different that the others except we are not going to run bigTest directly - first we have to get an access token and inject it, so we need to run dataStoreTest() which will do that, eventually getting round to running bigTest(). 

              "name": "DATASTORE",
              "functionName": "dataStoreTest",
              "skip": false,
              "options": {
                  "driver": "cDriverDataStore",
                  "parameters": {
                      "siloid": "polymerdbab",
                      "dbid": "xliberationdatastore",
                      "peanut": "bruce",
                      "disablecache": 1


function dataStoreTest (options, testData ) {
  // this will get an access token and pass it to doTheWork()
  return doGetPattern({} , constructConsentScreen, dataStoreTestRun,'googleDatastore',{options:options, data:testData} );

This is what htmlService will try to execute. If you recall from EzyOauth2 - taking some pain out of Apps Script API authentication, doGetPattern() simulates the authentication you'd expect with doGet() in a webapp without the need for user intervention by refreshing or reusing an unexpired access token. We also need to pass what doGetPattern() needs to do next - in this case call dataStoreTestRun(), and it has to pass the options and the testData that have been passed on from the profile and from the previous processing stage. 


function dataStoreTestRun (accessToken,pass) {  

   pass.options.parameters.accesstoken = accessToken;
   return bigTest (pass.options ,;

This will be called by doGetPattern() after it has collected a usable access token. All we need to do is inject it into the options and execute bigTest as normal - just like all the other database back end tests that didn't need authentication. 

And here's datastore running alongside all the other tests.

For more snippets like this see Google Apps Scripts snippets and for more on this topic, see Running things in parallel using HTML service

For help and more information join our forum,follow the blog or follow me on twitter .

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, available All formats are available now from O'Reilly,Amazon 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.