无效的请求使用@ microsoft / microsoft-graph-client创建订阅

时间:2019-06-23 15:45:37

标签: node.js microsoft-graph

我正在使用此javascript tutorial,并且在执行以下代码时出现错误:

const options = {
    authProvider,
};

const client = Client.init(options);

const subscription = {
   changeType: "created,updated",
   notificationUrl: "https://4714faf8.ngrok.io/events",
   resource: "me/events",
   expirationDateTime:"2019-06-24T18:23:45.9356913Z",
   clientState: "secretClientValue"
};

let res = await client.api('/subscriptions')
    .post({subscription : subscription});

我收到此错误:

statusCode: 400,
code: 'InvalidRequest',
message: 'expirationDateTime is a required property for subscription creation.',
requestId: 'c20f75b5-0dca-48d5-a116-a34124bbfd58',
date: 2019-06-23T19:42:56.000Z,
body: '{
    "code": "InvalidRequest",
    "message": "expirationDateTime is a required property for subscription creation.",
    "innerError": {
    "request-id": "c20f75b5-0dca-48d5-a116-a34124bbfd58",
    "date": "2019-06-23T15:42:56"
    }
}'

您知道原因吗?

2 个答案:

答案 0 :(得分:0)

您缺少"。节点resource: "me/events,应该是resource: "me/events",

const subscription = {
    changeType: "created,updated",
    notificationUrl: "https://4714faf8.ngrok.io/events",
    resource: "me/events",
    expirationDateTime: "2019-06-23T18:23:45.9356913Z",
    clientState: "secretClientValue"
};

修改

响应的日期/时间为2019-06-23T19:42:56.000Z,但是您请求的expirationDateTime2019-06-23T18:23:45.9356913Z。换句话说,您正在请求添加一个新的订阅,该订阅将在创建之前过期。

尝试将其设置为一两天:

const subscription = {
   changeType: "created,updated",
   notificationUrl: "https://4714faf8.ngrok.io/events",
   resource: "me/events",
   expirationDateTime:"2019-06-27T00:00:00Z",
   clientState: "secretClientValue"
};

答案 1 :(得分:0)

您必须直接将订阅对象传递给post函数:

let res = await client.api('/subscriptions').post(subscription);

对我有用。

const options = {
    authProvider,
};

const client = Client.init(options);

const subscription = {
   changeType: "created,updated",
   notificationUrl: "https://4714faf8.ngrok.io/events",
   resource: "me/events",
   expirationDateTime:"2019-06-24T18:23:45.9356913Z",
   clientState: "secretClientValue"
};

let res = await client.api('/subscriptions').post(subscription);

然后,Microsoft Graph验证您的请求,并将验证令牌发送回您的通知URL:
POST https://https://4714faf8.ngrok.io/events?validationToken={opaqueTokenCreatedByMicrosoftGraph}

您必须在10秒内发送具有以下特征的回复:

  • 200(OK)状态代码。
  • 内容类型必须为纯文本/纯文本。
  • 正文必须包含Microsoft Graph提供的验证令牌。

此通知端点验证过程在官方文档中进行了解释:https://docs.microsoft.com/en-us/graph/webhooks#notification-endpoint-validation