node.js - 使用 Directory API 获取资源日历
问题描述
我真的尝试过,但无法自己弄清楚如何使用 Directory API 检索资源日历的数据。
我想要的是:
- 使用具有域范围委派的服务帐户检索我公司资源日历的数据
我有的:
具有域范围委派的服务帐户(+ credentials.json)
我公司资源日历的日历ID
我是非管理员用户,据我所知,服务帐户必须模拟用户。我是否需要成为管理员用户才能管理和查看资源日历的数据并与服务帐户共享?我使用 jwtClient 进行授权,但我不知道是否必须使用 Directory API、Google Calendar API 或两者才能使其工作。
我首先尝试使用 Google Calendar API,但它不起作用,现在我想我必须使用 Directory API 获取资源日历。这就是我到目前为止所拥有的:
let express = require('express');
let app = express();
app.set('view engine', 'ejs');
let {google} = require('googleapis');
const calendar = google.calendar('v3');
const port = 3000;
const key = require('./credentials.json');
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
[ 'https://www.googleapis.com/auth/calendar'],
null
);
jwtClient.authorize(function(err, tokens) {
if (err) {
console.log(err);
} else {
console.log("Authorization worked!");
console.log(tokens);
}
});
app.get('/', function(req, res) {
calendar.events.list({
auth: jwtClient,
calendarId: 'xxxx@resourcexxx.com',
timeMin: (new Date()).toISOString(),
maxResults: 5,
singleEvents: true,
orderBy: 'startTime'
}, function(err, resp) {
res.json(resp);
});
});
app.use(express.static('public'));
app.listen(port, function(err){
if (!err) {
console.log("App is running on port", port);
} else {
console.log(JSON.stringify(err));
}
});
任何帮助,将不胜感激。提前致谢!
解决方案
I'm a non-admin user and as far as I know the service account has to impersonate a user. Do I need to be an admin user to manage and see the data of the resource calendar and share it with the service account?
Service accounts can impersonate users, when they have domain-wide delegation enabled. It is not necessary.
Service accounts behave like "normal" accounts - they have their own permissions on resources. That means that, if you want to get a specific calendar's data from that account, you will have to give it view permission. In case you would prefer to edit it using the service account, you will have to give the account edit permissions. You can do that as you would with any other user, the difference being that you will have to use the service account's e-mail address (which you will find in the developer's console) to reference it.
If you are using service account that has domain-wide authentication enabled, when authenticating, you can specify a "subject" (an existing user of your domain) to impersonate. After a successful authentication, every action performed will be executed as if the "subject" account was the one performing them - and it will use that user's permissions.
I use a jwtClient to authorize and I have no idea whether I have to use the Directory API, Google Calendar API or both to make it work.
- Since you are retrieving data from calendars (and not any user's information) you only need to use the Google Calendar API.
What I want: retrieve the data of my company's resource calendar by using a service account with domain-wide delegation
- In your case - retrieving data - you can simply share that calendar with your service account. Afterwards, with no need to use domain-wide delegation, it will be able to use
calendar.events.list
to retrieve the events present in the calendar along with their details, using the same code you provided.
Reference
推荐阅读
- selenium - Selenium 尝试弹出功能失败
- python - 从子类获取父类中的特定属性,而不将父类的属性传递给子类
- angular - y 所需的入口点 x 不存在
- flutter - 我更改了版本gradle并出现了这个问题
- sql - 在值和数量方面在 2 个表中找到完全相同的行
- vb.net - 面板控件卡在 visible=false
- html - 用css使按钮变圆
- xml - 如何在flutter的launch_background.xml中添加背景gif图像作为启动画面
- android-studio - Kotlin 类中的构造函数参数和成员变量有什么区别?
- javascript - 扩展组件的 React 类中未加载动态内容