Node.js 如何列出用户';使用Google日历API和NodeJS创建可用日历

Node.js 如何列出用户';使用Google日历API和NodeJS创建可用日历,node.js,google-calendar-api,Node.js,Google Calendar Api,我刚刚完成了谷歌关于如何使用日历API的讨论,一切都很好 现在我尝试获取用户订阅的所有日历的列表,似乎该方法正是我所需要的 我这样称呼它: calendar.calendarList.list( {}, (err, result) => console.log("Output: " + JSON.stringify(result)) ); const fs = require('fs'); const { google } = require('googleapis');

我刚刚完成了谷歌关于如何使用日历API的讨论,一切都很好

现在我尝试获取用户订阅的所有日历的列表,似乎该方法正是我所需要的

我这样称呼它:

calendar.calendarList.list(
    {},
    (err, result) => console.log("Output: " + JSON.stringify(result))
);
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))
    );
}
doc页面只列出可选参数,因此我不传递任何参数,并且按照教程中使用的第二个参数模式进行回调。不幸的是,我找不到更好的nodejsapi文档,所以我坚持这些模糊的假设

我的完整代码如下所示:

calendar.calendarList.list(
    {},
    (err, result) => console.log("Output: " + JSON.stringify(result))
);
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
那么,检索已订阅日历列表的正确方法是什么?我做错了什么


提前谢谢。

我认为你的脚本基本上是正确的。但是当
(err,result)=>console.log(“Output:+JSON.stringify(result)
result
未定义时,我认为可能会发生错误。那么这种确认和修改如何

确认要点:
  • Quickstart教程中使用的范围与使用脚本时使用的范围不同。
    • 这样,我认为新的作用域可能不会反映到访问令牌中。因此,请删除
      token.json
      一次,然后运行脚本并再次授权。这样,将重新创建
      token.json
      ,您可以将访问令牌用于新的作用域
  • 请再次确认是否在API控制台启用了日历API
修改点:
  • 在googleapis的最新版本中,为了从
    (err,result)=>console.log(“输出:“+JSON.stringify(result))
    检索值,请使用
    result.data
修改脚本: 对你的脚本进行这样的修改怎么样

发件人: 致:
如果这不是您的问题的解决方案,我很抱歉。

非常感谢!使用
结果。数据
而不是
结果
只能最终显示我想要的数据。这很奇怪,因为我认为我可以使用
JSON.stringify
来显示完整的对象,但它似乎不起作用。再次感谢!@ove谢谢你的回复。我很高兴你的问题得到了解决。