【发布时间】:2020-08-27 20:21:45
【问题描述】:
我想进行一些操作,以便我的 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.
我在互联网上翻了一堆,我不知道问题是我做错了什么还是问题是谷歌不支持我正在尝试做的事情。
这是我的代码:
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);
【问题讨论】:
-
你能提供你的净化代码吗?为了创建日历事件,您需要
https://www.googleapis.com/auth/calendar范围。服务帐户可能很棘手,因此如果没有尽可能多的信息,几乎不可能找到根本原因 -
@RafaGuillermo 感谢您的回复!我用我的代码更新了问题。
-
这似乎没问题。您是否为与您的服务帐户绑定的 GCP 项目启用了日历 API?此外,在
Security > Advanced Settings > Manage API client access下的admin console 中,确保您已为项目提供了日历范围。如果您不进行委派,请仔细检查您是否也邀请了服务帐户电子邮件到日历,(尽管这不应标记此错误,但值得进行健全性检查) -
这可能是问题所在!我是 GCP 项目的管理员,我只是仔细检查了日历 api 是否已启用。但我没有 G Suite 管理员权限。我什至无法访问管理控制台。我需要让 G Suite 管理员授予我的项目权限吗?
-
你会的,是的。服务帐户将需要在管理控制台中设置的范围,并且只有域管理员才能执行此操作。我在下面添加了这个作为答案
标签: google-calendar-api service-accounts