Enabling APIs and OAuth2


This is part of the writeup on Using Google Cloud Storage with Apps Script. In Setting up or creating a console project I showed how to create a project and to enable billing to be able to use Google Cloud Storage. The next step is to enable the APIS you'll need, and set up OAuth2. 

Enabling the APIS

In the Developers console entry for your project you'll see an API library. This shows all the APIS that you can enable from the console. 




In your dashboard you'll see the ones that are already enabled for your project. It may be that the ones you want are enabled by default. Here's how they look in my dashboard
If they are not enabled, then find them in the library and go through the enable dialog.

Setting up Oauth2

Oauth2 can be difficult to come to grips with. There are essentially two flows
  • One to use for access a users personal data. This explicitly gets his permission to do so via a web dialog
  • A service account, used to access data owned by the App. This normally runs from server based Apps. Since Apps Script is really server based and since you'll probably be using this to access shared data, I'm going to show the Service account method for this example
I'm using my cGoa library to handle all OAuth2, so there's very little coding you need to do. There are other libraries available, but they will all start with creating the credentials in the console, which is what we're about to do here. For other OAuth2 scenarios and information on my cGoa library see OAuth2 for Apps Script in a few lines of code

In the credentials section of the console, create new credentials

We're creating a service account, so the user doesn't need to go through an oauth dialog.

This service account will be the project owner, as per this dialog.

When you create this, it will download a json file. Move this somewhere private on your google drive and take a note of its file id. You don't need to look inside the file, as cGoa knows what to do with such a file, given its id.


Now the service account is set up and we can use it for future apps that need to access this project's storage as described in Using the service account to enable access to cloud storage





For more like this, see Using Google Cloud Storage with Apps Script Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.
Comments