Outlook使用NodeJS创建日历事件

问题描述

我需要使用NodeJS脚本在Outlook中创建日历事件。我已经搜索了所有地方并尝试了不同的npm软件包,但是我没有解决方案。

我正在尝试使用以下代码创建日历活动:

const rp = require('request-promise');

let token = "<access token>"

var jsonBody = {
    "Subject": "test event","Body": {
        "ContentType": "HTML","Content": "hello world"
    },"Start": {
        "DateTime": "2020-10-21T10:10:00","TimeZone": "India Standard Time"
    },"End": {
        "DateTime": "2020-10-21T11:10:00","location": {
        "displayName": "Noida"
    },"Attendees": [
        {
            emailAddress: {
                address: "yyy@yyy.com",name: "yyy yyy"
            },type: "required"
        },{
            emailAddress: {
                address: "yyy@yyy.com",type: "required"
        }
    ]
};
///me/calendar/events
var optionsForCreatingcalendar = {
    uri: 'https://outlook.office.com/api/v2.0/me/events',port: 443,method: 'POST',headers: {
        'Authorization': 'Bearer ' + token,'Content-Type': 'application/json'
    },json: true,body: jsonBody,resolveWithFullResponse: true,simple: false
};

// --- API call using promise-----
rp(optionsForCreatingcalendar)
    .then(function (response) {
        console.log(response);
    },function (err) {
        console.log(err);

    });

但是代码存在一些问题。它会抛出如下错误

 error: {
      code: 'UnabletoDeserializePostBody',message: 'were unable to deserialize '
    }

请帮助我上述代码中缺少的内容

谢谢

解决方法

https://github.com/jasonjoh/node-outlookhttps://www.npmjs.com/package/node-outlook

我想建议您那些库使用日历事件API。 如果您不想使用它,则需要通过Outlook API发送序列化数据。

创建事件的示例代码。

var outlook = require('node-outlook');

var newEvent = {
    "Subject": "Discuss the Calendar REST API","Body": {
        "ContentType": "HTML","Content": "I think it will meet our requirements!"
    },};

let createEventParameters = {
    token: ['access token will come here'],event: newEvent
};
outlook.calendar.createEvent(createEventParameters,function (error,event) {
    if(error) {
        console.log(error);                 
    } else {
        console.log(event);                         
    }
});

在您的情况下,您需要使用jsonBody而不是newEvent。然后它将起作用。

,
var outlook = require('node-outlook');

var jsonBody = {
    "Subject": "test event","Content": "hello world"
    },"Start": {
        "DateTime": "2020-10-21T10:10:00","TimeZone": "India Standard Time"
    },"End": {
        "DateTime": "2020-10-21T11:10:00","location": {
        "displayName": "Noida"
    },"Attendees": [
        {
            emailAddress: {
                address: "yyy@yyy.com",name: "yyy yyy"
            },type: "required"
        },{
            emailAddress: {
                address: "yyy@yyy.com",type: "required"
        }
    ]
};

let createEventParameters = {
    token: ['access token will come here'],event: jsonBody
};
outlook.calendar.createEvent(createEventParameters,event) {
    if(error) {
        console.log(error);                 
    } else {
        console.log(event);                         
    }
});

这是使用节点外观库的示例代码。