Google OAuth with Node.js
I've been working on an Alexa skill that needed access to my Google Calendar account. The provided quick start is a little tough to follow, so I worked through it and created a minimal example that generates an auth token and writes it to disk.
You'll need to install the google-auth-library first. Make sure that you're installing at least version 1.0.0 (the quick start installs 0.9 which doesn't work with the new Google SDK)
bash
Once that's installed, you'll need to create some OAuth credentials to used. Follow these instructions, taken from the quick start:
- Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials.
- On the Add credentials to your project page, click the Cancel button.
- At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.
- Select the Credentials tab, click the Create credentials button and select OAuth client ID.
- Select the application type Other, enter the name "Google Calendar API Quickstart", and click the Create button.
- Click OK to dismiss the resulting dialog.
- Click the file_download (Download JSON) button to the right of the client ID.
- Move this file to your working directory and rename it client_secret.json.
Next, you need to use those secrets to generate an access token. Create a file named auth.js with the following contents (we're accessing Google Calendar, but you can update SCOPES to contain anything you need).
javascript
Run node auth.js and visit the URL that's generated. Once you have a code, enter it in to the terminal and press enter. You should now have an access token saved to disk at ~/.credentials/calendar-nodejs-quickstart.json.