These articles are abridged from my book on Office to Apps migration. Going GAS, from VBA to Google Apps Script. Now available directly from O’Reilly or Amazon.
As with most VBA authentication processes on this site, authentication is handled through the VBA cOauth2 class and the VBA wrapper function getGoogled.
The project that contains the Apps Scripts to be executed remotely is the one that provides the scopes and the credentials, and can be accessed through the developers console entry for the project.
The Execution API needs to be enabled through the Developers console in every project that will be accessed remotely.
Just like a webapp, a version of the project needs to be saved and published, but this time using the ‘Deploy as API executable’ publishing option. The API ID matches the project key in the project properties (and is the same key as used when accessing as a library).
The API itself needs no particular scope. Instead, the authentication scopes are those required by the Apps Script project to be executed. In other words, the scopes specified when setting up getGoogled should match the scopes in the Apps Script project properties.
For the example in this demo, the only scope needed is spreadsheets.
Now you can set up the PC to be able to access this project, using cOAuth2 in VBA. This writes some information to the PC registry that is used behind the scenes for token management.
Firstly you need to add the project key to the known projects and define which scope(s) it’s going to need.
Case "...project key" .add "scope", URLEncode("https://www.googleapis.com/auth/spreadsheets")
For first time use, you need to provide the credentials. Since these credentials and scope are specific to the project being executed, the project key is used to store the credentials against, as in the example below.
This should be run once, then deleted (it’s neither a good idea nor necessary to keep credentials in code).
Private Function onceOff() getGoogled "...project key", , _ "......client id", _ "......secret" End Function
That’s it – now the project can be accessed by VBA
All code samples can be found on Github. Note that that VBA samples have been developed for Office for Windows and may need some tweaking for Office for Mac.
For more like this, see Execution API and Office to Apps migration
Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.
bruce mcpherson is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at http://www.mcpher.com. Permissions beyond the scope of this license may be available at code use guidelines