使用 GCP 服务帐户创建 Google 个日历事件
Creating Google Calendar events with a GCP Service Account
我想安装一些东西,以便我的 GCP 服务帐户可以邀请用户参加日历活动。因此,例如 my-service-account@myproject.iam.gserviceaccount.com
会邀请 user@myCorporation.com
参加活动。在我看来,这应该可以通过授予 my-service-account@myproject.iam.gserviceaccount.com
使用日历 API 的权限来实现,而无需 user@myCorporation.com
授予任何额外的权限。
我试图实现这个 example,但是用日历范围和日历 API 调用替换了计算范围和计算 API 调用。我的代码返回错误
Insufficient Permission: Request had insufficient authentication scopes.
我在网上查了很多,但我不知道问题是我做错了什么,还是 Google 不支持我正在尝试做的事情.
这是我的代码:
const {google} = require('googleapis');
const compute = google.compute('v1');
const {GoogleAuth} = require('google-auth-library');
async function main() {
const auth = new GoogleAuth({
scopes: ['https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/compute']
});
//keeping the compute stuff in as a sanity check
//to ensure that the problem is with calendar, not something more general
const authClient = await auth.getClient();
const project = await auth.getProjectId();
const res = await compute.zones.list({project, auth: authClient});
console.log(res.data);
createEvent(auth);
}
/**
* Lists the next 10 events on the user's primary calendar.
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function createEvent(auth) {
const calendar = google.calendar({version: 'v3', auth});
calendar.events.insert({
calendarId: 'primary',
event: {
"description": "my test event",
"start": {
"date": "2020-05-20",
},
attendees: [{email: "myGuest@mailinator.com"}]
}
}
);
}
main().catch(console.error);
答案:
您需要启用 API 并在三个位置提供范围:在您的授权代码中、在 GCP 控制台中和 Google 管理控制台中。
更多信息:
正如我在评论中解释的那样,您提供的代码应该 运行 没有问题。 Insufficient Permission: Request had insufficient authentication scopes.
错误是由于服务帐户未被授予对 Google 端某处所需范围的访问权限的结果。
确保您已完成以下步骤:
- 在应用程序中提供范围作为 auth 对象(您已经完成):
const auth = new GoogleAuth({
scopes: ['https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/compute']
});
- 在 GCP console 中为您的服务帐户 GCP 项目启用日历 API。
- 在 GCP 的 OAuth 同意屏幕设置中为您的服务帐户提供了所需的范围。
- 向 Google Admin console 中的服务帐户添加了所需的范围。这是通过遵循
Security > Advanced Settings > Manage API client access
UI 元素,并将服务帐户需要的 all 范围分配给服务帐户客户端 ID 来完成的。
注意:这最后一步必须由域管理员完成,任何非域管理员都无法完成。
在这种情况下,您需要联系您的域管理员以授予您的项目 API 访问权限。
参考文献:
相关回答:
我想安装一些东西,以便我的 GCP 服务帐户可以邀请用户参加日历活动。因此,例如 my-service-account@myproject.iam.gserviceaccount.com
会邀请 user@myCorporation.com
参加活动。在我看来,这应该可以通过授予 my-service-account@myproject.iam.gserviceaccount.com
使用日历 API 的权限来实现,而无需 user@myCorporation.com
授予任何额外的权限。
我试图实现这个 example,但是用日历范围和日历 API 调用替换了计算范围和计算 API 调用。我的代码返回错误
Insufficient Permission: Request had insufficient authentication scopes.
我在网上查了很多,但我不知道问题是我做错了什么,还是 Google 不支持我正在尝试做的事情.
这是我的代码:
const {google} = require('googleapis');
const compute = google.compute('v1');
const {GoogleAuth} = require('google-auth-library');
async function main() {
const auth = new GoogleAuth({
scopes: ['https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/compute']
});
//keeping the compute stuff in as a sanity check
//to ensure that the problem is with calendar, not something more general
const authClient = await auth.getClient();
const project = await auth.getProjectId();
const res = await compute.zones.list({project, auth: authClient});
console.log(res.data);
createEvent(auth);
}
/**
* Lists the next 10 events on the user's primary calendar.
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function createEvent(auth) {
const calendar = google.calendar({version: 'v3', auth});
calendar.events.insert({
calendarId: 'primary',
event: {
"description": "my test event",
"start": {
"date": "2020-05-20",
},
attendees: [{email: "myGuest@mailinator.com"}]
}
}
);
}
main().catch(console.error);
答案:
您需要启用 API 并在三个位置提供范围:在您的授权代码中、在 GCP 控制台中和 Google 管理控制台中。
更多信息:
正如我在评论中解释的那样,您提供的代码应该 运行 没有问题。 Insufficient Permission: Request had insufficient authentication scopes.
错误是由于服务帐户未被授予对 Google 端某处所需范围的访问权限的结果。
确保您已完成以下步骤:
- 在应用程序中提供范围作为 auth 对象(您已经完成):
const auth = new GoogleAuth({
scopes: ['https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/compute']
});
- 在 GCP console 中为您的服务帐户 GCP 项目启用日历 API。
- 在 GCP 的 OAuth 同意屏幕设置中为您的服务帐户提供了所需的范围。
- 向 Google Admin console 中的服务帐户添加了所需的范围。这是通过遵循
Security > Advanced Settings > Manage API client access
UI 元素,并将服务帐户需要的 all 范围分配给服务帐户客户端 ID 来完成的。
注意:这最后一步必须由域管理员完成,任何非域管理员都无法完成。
在这种情况下,您需要联系您的域管理员以授予您的项目 API 访问权限。