Как получить события календаря по имени календаря с помощью Microsoft-Graph и NodeJS? - PullRequest
0 голосов
/ 11 октября 2019

Как я могу сделать этот вызов API? Этот код использует microsoft-graph-client для двух вызовов API. Первый звонок получит 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;);

1 Ответ

1 голос
/ 14 октября 2019

Это выполнимо, поскольку к календарю можно обращаться по 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) {
    //...
}       
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...