繁体   English   中英

如何使用 Microsoft-Graph 和 NodeJS 按日历名称获取日历的事件?

[英]How to get calender's events by calendar name, with Microsoft-Graph and NodeJS?

我怎样才能打出这个 API 电话? 此代码使用 microsoft-graph-client 进行两个 api 调用。 第一次调用得到我想要的日历的 ID。 第二个使用日历的 ID 来获取该日历的事件。 想打一个 API 电话。 到目前为止,我阅读的所有文档都无法获取指定日历的日历事件。

//// 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;);

这是可行的,因为日历可以通过id及其name来寻址,如下所示:

GET /me/calendars/{id|name}

其中name对应于Calendar.name属性

因此事件可以通过这样的单个请求检索:

GET /me/calendars/{name}/calendarView?startDateTime={start_datetime}&endDateTime={end_datetime}

例子

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM