Created
August 9, 2018 04:28
-
-
Save Tolsee/7e8fdc12fb6679b69f5981957ec2bf6c to your computer and use it in GitHub Desktop.
Authenticate google calender with node.js.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const gEvent = require('vorpal')(); | |
const {google} = require('googleapis'); | |
const opn = require('opn'); | |
const CLIENT_ID = 'GOOGLE_CLIENT_ID'; | |
const CLIENT_SECRET = 'GOOGLE_CLIENT_SECRET'; | |
const REDIRECT_URL = 'urn:ietf:wg:oauth:2.0:oob'; | |
const oauth2Client = new google.auth.OAuth2( | |
CLIENT_ID, | |
CLIENT_SECRET, | |
REDIRECT_URL | |
); | |
// generate a url that asks permissions for Google Calendar scopes | |
const scopes = [ | |
'https://www.googleapis.com/auth/calendar', | |
'https://www.googleapis.com/auth/calendar.readonly' | |
]; | |
const url = oauth2Client.generateAuthUrl({ | |
access_type: 'offline', | |
scope: scopes | |
}); | |
gEvent | |
.command('connect') | |
.description('Connect to your google account') | |
.action(function (args, callback) { | |
opn(url); | |
callback(); | |
}); | |
gEvent | |
.command('authorize <code>') | |
.description('Authorize your account with token') | |
.action(async function (args, callback) { | |
const {tokens} = await oauth2Client.getToken(code) | |
oauth2Client.setCredentials(tokens); | |
callback(); | |
}); | |
gEvent | |
.delimiter('gevent $') | |
.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment