【问题标题】:Google Calendar API and Service Account permission errorGoogle Calendar API 和服务帐户权限错误
【发布时间】:2020-07-03 09:12:37
【问题描述】:

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

  • 在 Cloud Platform 上创建了一个新项目
  • 启用日历 API
  • 添加了一个角色为所有者的新服务帐号
  • 生成的 jwt.json
  • 为该服务帐户授予域范围
  • 与该服务帐户共享日历(修改权限)
  • 在 GSuite 中启用了让组织外的每个人都可以修改事件的选项

现在,我在 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(“服务器错误”)响应: 错误:权限不足

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

【问题讨论】:

    标签: node.js jwt google-calendar-api


    【解决方案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的错误信息可能是这个原因。

    模式 2:

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

    修改脚本:

    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

    参考:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 2020-08-11
      相关资源
      最近更新 更多