Я только что закончил Google Quickstart Tutorial о том, как использовать Calendar API, и все отлично работало.
Теперь я пытаюсь получить список всех календарей, на которые подписан пользователь, ипохоже, что метод calendarList.list
делает именно то, что мне нужно.
Я называю это так:
calendar.calendarList.list(
{},
(err, result) => console.log("Output: " + JSON.stringify(result))
);
На странице документа перечислены только дополнительные параметры, поэтому яне передавайте ничего, и я следую шаблону callback-as-second-parameter, как использовано в руководстве.К сожалению, мне не удалось найти более качественную документацию по API nodejs, поэтому я придерживаюсь этих смутных предположений.
Мой полный код выглядит так:
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))
);
}
Этот скрипт возвращает следующий вывод:
Output: undefined
Итак, как правильно получить список подписанных календарей?Что я делаю не так?
Заранее спасибо.