[英]How to get calender's events by calendar name, with Microsoft-Graph and NodeJS?
How can I make this one API call?我怎样才能打出这个 API 电话? This code uses microsoft-graph-client to make two api calls.
此代码使用 microsoft-graph-client 进行两个 api 调用。 The frist call gets the ID of the calender I want.
第一次调用得到我想要的日历的 ID。 The second uses the calendar's ID to get the events of that calendar.
第二个使用日历的 ID 来获取该日历的事件。 Would like to have one API call.
想打一个 API 电话。 All the documentation I have read so far does not have ability to get calendar events of a specified calendar.
到目前为止,我阅读的所有文档都无法获取指定日历的日历事件。
//// Modules used...
var authHelper = require('../helpers/auth'); //// used to get the access token
var graph = require('@microsoft/microsoft-graph-client');
... ...
//// Initialize Graph client with access token.
const client = graph.Client.init({
authProvider: (done) => {
done(null, accessToken);
}
});
//// Specify start and end times for second API call.
const start = new Date(new Date().setHours(0,0,0));
const end = new Date(new Date(start).setDate(start.getDate() + 30000));
/**
* Step 1
* Get all the calendar, then cut out the calendar id I need.
* STEP 2
* Get the events using the calendar id.
*/
const calendars = await client
.api('https://graph.microsoft.com/v1.0/me/calendars/')
.select('name,id')
.get();
/**
* Cut out the id of first calendar named 'School_Calendar' in the array of calendars.
*/
const c = calendars.value.find(obj => {
return obj.name === 'School_Calendar'
});
const calen_id = c.id;
/**
* Now use the Calendar's id to get the calendar's events.
*/
const api = `/me/calendars/${calen_id}/calendarView?startDateTime=${start.toISOString()}&endDateTime=${end.toISOString()}`;
const result = await client
.api(api)
.select('subject,start,end')
.orderby('start/dateTime DESC')
.get();
//// print the events of School_Calendar
console.log(result.value;);
That's doable since calendar could be addressed by id
and its name
like this:这是可行的,因为日历可以通过
id
及其name
来寻址,如下所示:
GET /me/calendars/{id|name}
where name
corresponds to a Calendar.name
property其中
name
对应于Calendar.name
属性
Hence events could retrieved via a single request like this:因此事件可以通过这样的单个请求检索:
GET /me/calendars/{name}/calendarView?startDateTime={start_datetime}&endDateTime={end_datetime}
Example例子
const data = await client
.api(
`/users/${userId}/calendars/${calName}/calendarView?startDateTime=${start.toISOString()}&endDateTime=${end.toISOString()}`
)
.get()
const events = data.value;
for (let event of events) {
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.