I'm following the getting started with "Authentication and authorization" using OAuth2 section using the official guide at https://github.com/googleapis/google-api-nodejs-client#installation
Following these very simple few lines of code simply fails to work. I'm redirected correctly, I then sign-in and allow the requested scopes. At this point I get redirected to the callback URL, I parse the code from query string successfully and pass it into my OAuth2Client object by calling oauth2Client.getToken(code). This is the point where I'm completely lost because no matter what this method always returns 'undefined'. No errors are thrown at all it's just 'undefined'. Not very helpful to say the least.
const {google} = require('googleapis');
const oauth2Client = new google.auth.OAuth2(
'hidden',
'hidden',
'hidden'
);
/* GET home page. */
router.get('/home', function(req, res, next) {
try {
if (req.url.indexOf('/home') > -1) {
var code = req.query.code;
var error = req.query.error;
var session = req.session;
if(code!=null){
const {tokens} = oauth2Client.getToken(code); //Always returns undefined?
oauth2Client.credentials = tokens;
}
else{
res.redirect('/');
}
}
} catch (e) {
res.redirect('/err')
}
});