Google 日历 API 和服务帐户权限错误

Google Calendar API and Service Account permission error

我正在尝试将 Google 日历 API 集成到我的应用程序中。 到目前为止,我已经设法做到了:

现在,我在 node.js 上的代码如下所示:

const { JWT } = require('google-auth-library');

const client = new JWT(
    keys.client_email,
    null,
    keys.private_key,
    ['https://www.googleapis.com/auth/calendar']
);

const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
const rest = await client.request({url});
console.log(rest);

我得到的错误是:

发送 500 ("Server Error") 响应: 错误:权限不足

有人有什么想法吗?这让人沮丧。

这个修改怎么样?

我认为在您的脚本中,端点 and/or 范围可能不正确。

模式 1:

在此模式中,使用您的端点 https://dns.googleapis.com/dns/v1/projects/${keys.project_id}

修改后的脚本:

const { JWT } = require("google-auth-library");
const keys = require("###");  // Please set the filename of credential file of the service account.

async function main() {
  const calendarId = "ip15lduoirvpitbgc4ppm777ag@group.calendar.google.com";
  const client = new JWT(keys.client_email, null, keys.private_key, [
    'https://www.googleapis.com/auth/cloud-platform'  // <--- Modified
  ]);
  const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
  const res = await client.request({ url });
  console.log(res.data);
}

main().catch(console.error);
  • 在这种情况下,需要在 API 控制台启用 Cloud DNS API。而且是需要付费的。请注意这一点。
    • 我认为你的Insufficient Permission错误信息的原因可能是这个。

模式二:

在此模式中,作为示例情况,事件列表是从与服务帐户共享的日历中检索的。如果日历可以与服务帐户一起使用,则返回事件列表。至此,我想你可以确认脚本是否有效。

修改后的脚本:

const { JWT } = require("google-auth-library");
const keys = require("###");  // Please set the filename of credential file of the service account.

async function main() {
  const calendarId = "###";  // Please set the calendar ID.

  const client = new JWT(keys.client_email, null, keys.private_key, [
    "https://www.googleapis.com/auth/calendar"
  ]);
  const url = `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`;  // <--- Modified
  const res = await client.request({ url });
  console.log(res.data);
}

main().catch(console.error);

注:

  • 此修改后的脚本假定您使用的是最新版本的google-auth-library-nodejs

参考:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。