node.js - 如何使用 Google Calendar API 和 NodeJS 列出用户的可用日历

标签 node.js google-calendar-api

我刚刚完成了 Google 的 Quickstart Tutorial关于如何使用日历 API,一切都运行得很好。

现在我尝试获取用户订阅的所有日历的列表,看起来像 calendarList.list方法正是我所需要的。

我这样调用它:

calendar.calendarList.list(
    {},
    (err, result) => console.log("Output: " + JSON.stringify(result))
);

文档页面仅列出可选参数,因此我不会传递任何参数,并且遵循教程中使用的回调作为第二参数模式。不幸的是,我无法找到更好的 Nodejs API 文档,所以我坚持这些模糊的假设。

我的完整代码如下所示:

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)resultundefined 时,我觉得可能会出现错误,那么这个确认并修改怎么样?

确认点:

  • 快速入门教程中使用的范围与使用脚本的范围不同。
    • 由此,我认为新范围可能不会反射(reflect)到访问 token 中。因此,请删除一次token.json,然后运行脚本并再次授权。这样,token.json 就会重新创建,您可以在新范围内使用访问 token 。
  • 请再次确认是否在 API 控制台启用了 Calendar API。

修改点:

  • 在最新版本的 googleapis 中,为了从 (err, result) => console.log("Output: "+ JSON.stringify(result)) 检索值,请使用结果.data.

修改后的脚本:

对你的脚本进行这样的修改怎么样?

从:
(err, result) => console.log("Output: " + JSON.stringify(result))
到:
(err, result) => {
  if (err) {
    console.log(err);
  } else {
    console.log("Output: " + result.data); // or JSON.stringify(result.data)
  }
}

如果这不能解决您的问题,我很抱歉。

关于node.js - 如何使用 Google Calendar API 和 NodeJS 列出用户的可用日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52578720/

相关文章:

node.js - 在nestjs中启动应用程序后是否可以更改CacheModule ttl?

node.js - 如何在 nodejs 中编写 KSQL 查询?

html - Nodejs Express 不加载脚本文件

google-calendar-api - 在谷歌日历(API)中插入多个 calendar.events

javascript - 为什么后续的链式 promise 得到解决?

node.js - 如何递归地遍历目录,通过 node.js 中的套接字发送所有文件名?

php - 如何更改 Google 日历嵌入事件的颜色?

javascript - Google Cal API 返回不完整的事件资源

python - 是否可以更新 Google 服务帐户的设置?

php - 使用 Google API for Google Calendar v3 刷新 token