简体   繁体   English

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

[英]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.

相关问题 如何使用Microsoft-Graph API中的文件名获取Excel-File ID? - How to get the Excel-File id using the filename in microsoft-graph api? 如何使用 Microsoft 图形添加多个日历事件 - How to add multiple calendar events using Microsoft graph 如何在调整日历中的事件大小及其各自的事件名称后获取开始时间和结束时间? - how to get start time and end time after resizing the events in calender and also its respective event name? 是否可以将Microsoft-Graph客户机密放入源代码中? - Is it possible to put microsoft-graph client secret into source code? 访问日历事件时使用Microsoft Graph API 401 - Microsoft Graph API 401 when accessing calendar events 全日历Jquery,自动将事件插入日历 - full calendar Jquery, Auto Insert Events in to the calender 如何使用 Microsoft Graph API 获取用户的照片(个人资料)? - How to get user's photo(profile) using Microsoft Graph API? 如何使用 nodejs 和 node-fetch 从 Azure 为 Microsoft Graph 获取客户端令牌 - How to get a client token from Azure for Microsoft Graph using nodejs and node-fetch 如何显示jQuery Full Calendar事件 - how to get jQuery Full Calendar events to show 如何使用 JavaScript 从 Google Calendar (API) 获取今天的事件? - How to get today's events from Google Calendar (API) using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM