簡體   English   中英

Google Calendar API 和服務帳戶權限錯誤

[英]Google Calendar API and Service Account permission error

我正在嘗試將 Google Calendar API 集成到我的應用程序中。 到目前為止,我已經設法做到了:

  • 在雲平台上創建了一個新項目
  • 啟用日歷 API
  • 添加了一個具有角色的新服務帳戶:所有者
  • 生成的 jwt.json
  • 為該服務帳戶授予全域權限
  • 與該服務帳戶共享日歷(修改權限)
  • 在 G Suite 中為組織外的每個人啟用修改事件的選項

現在,我在 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(“服務器錯誤”)響應:錯誤:權限不足

任何人有任何想法? 這令人沮喪。

這個改裝怎么樣?

我認為在您的腳本中,端點和/或范圍可能不正確。

模式一:

在此模式中,使用您的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

參考:

如果我誤解了您的問題並且這不是您想要的方向,我深表歉意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM