This describes how to set up a script to use the Goa library as described in Oauth2 for Apps Script in a few lines of code (which you should read first for background).
The library, cGoa, is available under this project key, or on github. All the tests described for this topic are also available on github.
Goa needs the credentials package to be stored in the property store to create a dialog, or to refresh an access token automatically if it expires. GoaApp provides a function to set these. Firstly these should be obtained from the App dashboard of the service you want to use.
This example shows the App dashboard for Microsoft Live, to allow the use of OneDrive from Apps Script, from which you can get the client id and client secret.
Here is how to set up the package. This should be executed once. You can then remove it from your script to avoid having credentials stored in scripts you might want to share with others.
|packageName||A package is the credentials , service, scope and control data used to authenticate. You should give each package a unique name|
|clientId||The id from the service's app dashboard. Some services have names other than clientId (such as appId), but you should set the clientId with the value from the service's dashboard. If you are using a service account, this does not need to be provided.|
|clientSecret||The secret from the service's app dashboard. If you are using a service account, this does not need to be provided .|
|scopes||An array of scopes which define the specific types of access required to the service. Each service has a different list and format of scopes. They should be entered here as an array of strings. If a service doesnt use scopes, just put an empty array here ().|
|service||This is the name of service (as it is known by Goa). Currently known service names can be obtained here. The service name for Microsoft is 'live'|
|fields for service account|| Optional. If you are using a service account in place of the normal web OAuth2 flow, these need an additional set of properties such as encryption keys. To avoid adding them manually, a function is available to set them from the service account JSON file which can be downloaded from the Developer Console when the service account is first created by simply supplying the fileid of the downloaded JSON.file as below. In this case only the package name, service name and scopes are needed. Client ID and Client Secret are extracted from the JSON key file. Note that your DriveApp is passed as an argument. This avoids an unnecessary scope requirement for users of this library who don't need to use service accounts.|
Now you can use Goa to manage OAuth2 for this script by simply providing the property store and package name.
For more like this, see Oauth2 for Apps Script in a few lines of code. 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