Created
May 8, 2015 18:54
-
-
Save Gofilord/144183a22b0f4d9c4789 to your computer and use it in GitHub Desktop.
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
var google = require('googleapis'); | |
var OAuth2 = google.auth.OAuth2; | |
// constants | |
var CLIENT_ID = '312634809793-4d4baf8u62ck2l4s7uucamp175m9q49p.apps.googleusercontent.com'; | |
var CLIENT_SECRET = 'My Client Secret'; | |
var REDIRECT_URL = 'http://localhost:3000/oauth2callback'; // registered with the developer console | |
var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL); | |
var url = oauth2Client.generateAuthUrl({ | |
access_type: 'offline', | |
scope: 'https://www.googleapis.com/auth/calendar.readonly' // which api I will be using | |
}); | |
console.log('url: ' + url); // works | |
/* GET home page. */ | |
router.get('/', function(req, res) { | |
res.render('index', { url: url }); | |
}); | |
router.get('/oauth2callback', function(req, res) { | |
var code = req.query.code; | |
oauth2Client.getToken(code, function(err, tokens) { | |
// Now tokens contains an access_token and an optional refresh_token. Save them. | |
if (!err) { | |
oauth2Client.setCredentials(tokens); | |
var calendar = google.calendar({ version: 'v3', auth: oauth2Client }); | |
console.log('calendar: ' + JSON.stringify(calendar)); // all well except no actualy values | |
console.log('events: ' + JSON.stringify(calendar.events)); // --> events: {} | |
var list = calendar.events.list({ | |
calendarId: '[email protected]', | |
maxResults: 10 | |
}, function(err, results) { | |
if (err) return console.log(err); | |
res.render('index', { url: url, events: results }); | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment