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",
"TimeZone": "India Standard Time"
},
"location": {
"displayName": "Noida"
},
"Attendees": [
{
emailAddress: {
address: "yyy@yyy.com",
name: "yyy yyy"
},
type: "required"
},
{
emailAddress: {
address: "yyy@yyy.com",
name: "yyy yyy"
},
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-outlook或https://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",
"Body": {
"ContentType": "HTML",
"Content": "hello world"
},
"Start": {
"DateTime": "2020-10-21T10:10:00",
"TimeZone": "India Standard Time"
},
"End": {
"DateTime": "2020-10-21T11:10:00",
"TimeZone": "India Standard Time"
},
"location": {
"displayName": "Noida"
},
"Attendees": [
{
emailAddress: {
address: "yyy@yyy.com",
name: "yyy yyy"
},
type: "required"
},
{
emailAddress: {
address: "yyy@yyy.com",
name: "yyy yyy"
},
type: "required"
}
]
};
let createEventParameters = {
token: ['access token will come here'],
event: jsonBody
};
outlook.calendar.createEvent(createEventParameters, function (error, event) {
if(error) {
console.log(error);                 
} else {
console.log(event);                         
}
});

这是使用nodeoutlook库的示例代码。

请对jsonBody中的所有属性使用PascalCase。这会奏效的。

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",
TimeZone: "India Standard Time",
},
Location: {
DisplayName: "Noida",
},
Attendees: [
{
EmailAddress: {
Address: "yyy@yyy.com",
Name: "yyy yyy",
},
Type: "required",
},
{
EmailAddress: {
Address: "yyy@yyy.com",
Name: "yyy yyy",
},
Type: "required",
},
],
};

最新更新