簡體   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