Google calendar api 在nodejs应用程序中调用日历列表时不返回结果

Google calendar api 在nodejs应用程序中调用日历列表时不返回结果,google-calendar-api,Google Calendar Api,因此,上面的代码对启用了日历api的帐户中的日历列表进行jwt授权调用。当我转到控制台时,我甚至可以看到成功的请求,但是我从nodejs得到的结果是一个空数组,好像我在api资源管理器中测试它一样,我得到了正确的结果。。发生什么事了 这是一个有一年历史的问题,但也许其他人会从这个答案中受益。您应该在代码中使用“数据”而不是“项目”。下面是一个工作示例: let scopes = ["https://www.googleapis.com/auth/calendar", "https://www.g

因此,上面的代码对启用了日历api的帐户中的日历列表进行jwt授权调用。当我转到控制台时,我甚至可以看到成功的请求,但是我从nodejs得到的结果是一个空数组,好像我在api资源管理器中测试它一样,我得到了正确的结果。。发生什么事了

这是一个有一年历史的问题,但也许其他人会从这个答案中受益。您应该在代码中使用“数据”而不是“项目”。下面是一个工作示例:

let scopes = ["https://www.googleapis.com/auth/calendar", "https://www.googleapis.com/auth/calendar.readonly"];
if (checker(googleKey)&&checker(jwtClient)) {
    jwtClient = new google.auth.JWT(
        googleKey.client_email,
        null,
        googleKey.private_key, scopes,
        null
    );
        jwtClient.authorize(function jwtAuth(err, tokens) {
            if (err) {
                res.json({ error: "Failed to execute auth command " });
                return;
            }

            let calendar = google.calendar('v3');
            calendar.calendarList.list({
                auth: jwtClient
            }, function(err, resp) {
                if (err || !resp.items) {
                    res.json({ error: "Failed to execute calendar list request " });
                    return;
                }
                let calendars = resp.items.map(cal => { if (cal.summary) return { id: cal.id, txt: cal.summary } });
                res.json(calendars);
            });
        });
    }

基于此,如果您有一个空数组,并使用一个大的数字对其进行索引,那么该数字之前的数组中的所有插槽都将填充未定义的(在JSON中转换为null)。此外,你可以参考。如果您想授予服务帐户访问您的谷歌日历的权限,您需要获取服务帐户电子邮件地址,并将您的谷歌日历与之共享,就像您与任何其他用户共享一样。希望这有帮助!谢谢,我的问题是我没有共享日历的访问权限。我用过这个。我不知道你在谈论数组索引问题,我的代码清晰地调用了谷歌数据的映射。我不是直接指任何数组索引。嘿。。谢谢你的回复。我会试试看,然后回来。
async function listCalendars() {
  // console.log( calendar.calendarList);
  calendar.calendarList.list({
      auth: oauth2Client,
      maxResults: 100
    },
    function (err, result) {
      console.log(result.data.items);
    }
  );
}

listCalendars().catch(console.error);