NodeJS 在创建或更新事件时通过Outlook日历的WebHooks接收更改通知中的空请求正文

6vl6ewon  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(135)

我希望在outlook calendar中创建、更新或删除事件时收到更改通知。
我已创建订阅

var options = {
  method: "POST",
  headers: { "content-type": "application/json", Authorization: `Bearer ${token}` },
  body: JSON.stringify({
    "changeType": "created,updated",
    "notificationUrl": "https://c5d1-2405-201-d003-d970-8dc3-6e92-3617-e572.ngrok-free.app/notification",
    "lifecycleNotificationUrl": "https://c5d1-2405-201-d003-d970-8dc3-6e92-3617-e572.ngrok-free.app/notification",
    "resource": "/me/events",
    "expirationDateTime": new Date(Date.now() + 3 * 24 * 3600 * 1000).toISOString(),
    "clientState": "alok"
  })
}
const response = await fetch('https://graph.microsoft.com/v1.0/subscriptions', options)
const responseData = await response.json()
console.log(responseData)

我在创造,

{
  '@odata.context': 'https://graph.microsoft.com/v1.0/$metadata#subscriptions/$entity',
  id: 'fd3a73f4-40df-40a8-bcc3-6510037cbeaf',
  resource: '/me/events',
  applicationId: 'e28030f7-6516-4229-b4d7-0db55c4db948',
  changeType: 'created,updated',
  clientState: 'alok',
  notificationUrl: 'https://c5d1-2405-201-d003-d970-8dc3-6e92-3617-e572.ngrok-free.app/notification',
  notificationQueryOptions: null,
  lifecycleNotificationUrl: 'https://c5d1-2405-201-d003-d970-8dc3-6e92-3617-e572.ngrok-free.app/notification',
  expirationDateTime: '2023-06-22T07:15:26.177Z',
  creatorId: '00064000A077365C',
  includeResourceData: null,
  latestSupportedTlsVersion: 'v1_2',
  encryptionCertificate: null,
  encryptionCertificateId: null,
  notificationUrlAppId: null
}

这是我的express.js基于webhook handler

app.post('/notification', async (req, res) => {
  if (req.query && req.query.validationToken) {
    res.set('Content-Type', 'text/plain');
    res.send(req.query.validationToken);
    return;
  }
  console.log('req.body', req.body)
  console.log('req.headers', req.headers)
  res.send({});
});

创建事件时,我收到通知

req.body undefined
req.headers {
  host: 'c5d1-2405-201-d003-d970-8dc3-6e92-3617-e572.ngrok-free.app',
  'content-length': '828',
  'content-type': 'application/json; charset=utf-8',
  'request-id': '3c22ec6a-2467-4628-b1ce-85f8394d58e8',
  'request-timestamp': '6/19/2023 7:19:25 AM +00:00',
  'x-forwarded-for': '40.76.162.99',
  'x-forwarded-proto': 'https',
  'accept-encoding': 'gzip'
}

我缺少了什么,所以我没有在正文中收到更改通知数据(为什么req.body是空的?))?

e4yzc0pl

e4yzc0pl1#

body-parser中间件添加到express.js应用程序将解决问题。

const express = require('express');
var bodyParser = require('body-parser')

const app = express();
app.use(bodyParser.json({ limit: "50mb" }));

app.post('/notification', async (req, res) => {
  if (req.query && req.query.validationToken) {
    res.set('Content-Type', 'text/plain');
    res.send(req.query.validationToken);
    return;
  }
  console.log('req.body', req.body)
  console.log('req.headers', req.headers)
  res.send({});
});

// Start the server
app.listen(3000, () => {
  console.log('Server started on http://localhost:3000');
});

相关问题