Reputation: 11205
Based on the nodejs quickstart sample (the credentials are replaced with dummy ones) here
Following is the code that works:
const fs = require('fs');
const mkdirp = require('mkdirp');
const readline = require('readline');
const {google} = require('googleapis');
const OAuth2Client = google.auth.OAuth2;
const SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'];
const TOKEN_PATH = 'credentials.json';
// Load client secrets from a local file.
fs.readFile('client_secret.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Drive API.
authorize(JSON.parse(content), listEvents);
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.web;
const oAuth2Client = new OAuth2Client(client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getAccessToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
// THIS WORKS IF ITS HERE AND PASSED IN CALLBACK
let client = new OAuth2Client("484407463774-e7biatgmpns9jcpakr5g0sed8fab376u.apps.googleusercontent.com", "722_fI1u2abNM3tL-VbCuZfF", "http://localhost:1337/api/v1/oauthCallback");
client.setCredentials({
"access_token": "ya29.GluyBSUYvP_Gi4_SdJHabcJmXUjHnw34MfMBJ8tzROflqyR9dFMDOh_AYh9dmL4FSNDiva_nAcWYCM9m5jBwaL3pWfSm_wv0IybUUdebt66gDakdFXL0o8Mr-0Ge",
"expiry_date": 1525551674971,
"token_type": "Bearer",
"refresh_token": "1/Ug8agC92PkJRXEDLP1inlHcAh4MBP1SLjNoylPJrmfg"
});
callback(client);
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return callback(err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client).catch(console.error);
});
});
}
/**
* Lists the next 10 events on the user's primary calendar.
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listEvents(auth) {
const calendar = google.calendar({ version: 'v3', auth });
calendar.events.list({
calendarId: 'primary',
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
}, (err, {data}) => {
if (err) return console.log('The API returned an error: ' + err);
const events = data.items;
if (events.length) {
console.log('Upcoming 10 events:');
events.map((event, i) => {
const start = event.start.dateTime || event.start.date;
console.log(`${start} - ${event.summary}`);
});
} else {
console.log('No upcoming events found.');
}
});
}
So in above code, we are first finding the credentials from credentials.json file. I have simply hardcoded them for convenience in authorize()
let client = new OAuth2Client("484407463774-e7biatgmpns9jcpakr5g0sed8fab376u.apps.googleusercontent.com", "722_fI1u2abNM3tL-VbCuZfF", "http://localhost:1337/api/v1/oauthCallback");
client.setCredentials({
"access_token": "ya29.GluyBSUYvP_Gi4_SdJHabcJmXUjHnw34MfMBJ8tzROflqyR9dFMDOh_AYh9dmL4FSNDiva_nAcWYCM9m5jBwaL3pWfSm_wv0IybUUdebt66gDakdFXL0o8Mr-0Ge",
"expiry_date": 1525551674971,
"token_type": "Bearer",
"refresh_token": "1/Ug8agC92PkJRXEDLP1inlHcAh4MBP1SLjNoylPJrmfg"
});
But if I move the above lines in listEvents()
like this:
const fs = require('fs');
const mkdirp = require('mkdirp');
const readline = require('readline');
const {google} = require('googleapis');
const OAuth2Client = google.auth.OAuth2;
const SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'];
const TOKEN_PATH = 'credentials.json';
// Load client secrets from a local file.
fs.readFile('client_secret.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Drive API.
authorize(JSON.parse(content), listEvents);
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.web;
const oAuth2Client = new OAuth2Client(client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getAccessToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(null);
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return callback(err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client).catch(console.error);
});
});
}
/**
* Lists the next 10 events on the user's primary calendar.
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listEvents(auth) {
// THIS DOES NOT WORK
let client = new OAuth2Client("480557463774-e7biatgmpns9jcpakran0sed8f55376u.apps.googleusercontent.com", "722_fBsu2ECNM3tL-VbCuZfF", "http://localhost:1337/api/v1/oauthCallback");
client.setCredentials({
"access_token": "ya29.GluyBSUYvP_Gi4_SdJHsuPJmXUjHdd34MfMBJ8tzROflqyR9dFMDOh_AYh9dmL4FSNDiva_nAcWYCM9m5jBwaL3pWfSm_wv0IybUUORbt66gDakdFXL0o8Mr-0Ge",
"expiry_date": 1525551674971,
"token_type": "Bearer",
"refresh_token": "1/Ug8MhC92ddJRXEDLP1inlHcAh4MBP1SLjNoylPJrmfg"
});
const calendar = google.calendar({ version: 'v3', client });
calendar.events.list({
calendarId: 'primary',
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
}, (err, {data}) => {
if (err) return console.log('The API returned an error: ' + err);
const events = data.items;
if (events.length) {
console.log('Upcoming 10 events:');
events.map((event, i) => {
const start = event.start.dateTime || event.start.date;
console.log(`${start} - ${event.summary}`);
});
} else {
console.log('No upcoming events found.');
}
});
}
Then it does not work any more and gives this error:
(node:95252) UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property `data` of 'undefined' or 'null'.
at calendar.events.list (C:\Users\rahulserver\Desktop\gcalapinodejs\quickstart.js:87:6)
at C:\Users\rahulserver\Desktop\gcalapinodejs\node_modules\google-auth-library\build\src\transporters.js:74:17
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:95252) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside o
f an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:95252) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections tha
t are not handled will terminate the Node.js process with a non-zero exit code.
IMO I am doing the same thing in either case. In first case, I am passing the client as a parameter to the callback. In second, I am passing null to the callback, and instantiating the oauth client in the callback itself. Not sure what "Mysterious" thing is happening in second approach that makes the code not work any more.
Upvotes: 0
Views: 341
Reputation: 11205
Well I figured it out.
Instead of
const calendar = google.calendar({ version: 'v3', client });
It should be
const calendar = google.calendar({ version: 'v3', auth: client });
So es6 object shorthand syntax can be confusing at times!
Upvotes: 0
Reputation: 6908
You're getting some kind of error, which makes the second arg in the callback from calendar.events.list()
undefined
.
Keep in mind this callback takes the form (err, {data}) => {
, so if there is an error, you won't have a second arg. However, you're still trying to destructure it, hence your error.
Simple fix is to apply a default argument. This'll make it use an empty object in the event of an error:
(err, { data } = {}) => {
Alternatively, you could destructure it after the error-check. Personally I'm a fan of default args.
Once you've made that change, you'll be able to see what the err is. Hopefully that message will make the remaining issue easy to fix.
Upvotes: 1