Reputation: 78
I just completed Google's Quickstart Tutorial on how to use the Calendar API and everything worked perfectly fine.
Now I try to get a list of all calendars the user is subscribed to and it seems like the calendarList.list
method does exactly what I need.
I call it like this:
calendar.calendarList.list(
{},
(err, result) => console.log("Output: " + JSON.stringify(result))
);
The doc page lists only optional parameters so I do not pass any and I follow the callback-as-second-parameter-pattern as used in the tutorial. Unfortunately, I was unable to find a better documentation of the nodejs API so I stick with these vague assumptions.
My full code looks like this:
const fs = require('fs');
const { google } = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
const TOKEN_PATH = 'token.json';
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
authorize(JSON.parse(content), start);
});
function authorize(credentials, callback) {
const { client_secret, client_id, redirect_uris } = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getAccessToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
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 console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
function start(auth) {
const calendar = google.calendar({ version: 'v3', auth: auth });
calendar.calendarList.list(
{},
(err, result) => console.log("Output: " + JSON.stringify(result))
);
}
This script returns the following output:
Output: undefined
So what's the correct way to retrieve a list of subscribed calendars? What am I doing wrong?
Thanks in advance.
Upvotes: 3
Views: 2520
Reputation: 201388
I think that your script is basically correct. But when result
of (err, result) => console.log("Output: " + JSON.stringify(result)
is undefined
, I think that an error might occur. So how about this confirmation and modification?
token.json
once, and run the script and authorize again. By this, token.json
is recreated and you can use the access token with the new scope.(err, result) => console.log("Output: " + JSON.stringify(result))
, please use result.data
.How about this modification of your script?
From:(err, result) => console.log("Output: " + JSON.stringify(result))
To:
(err, result) => {
if (err) {
console.log(err);
} else {
console.log("Output: " + result.data); // or JSON.stringify(result.data)
}
}
If this was not the solution of your issue, I'm sorry.
Upvotes: 1