【问题标题】:Outlook Create calendar event with NodeJSOutlook 使用 NodeJS 创建日历事件
【发布时间】:2020-10-20 07:46:56
【问题描述】:

我需要使用 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 '
    }

请帮助我在上面的代码中缺少什么。

谢谢

【问题讨论】:

    标签: node.js microsoft-graph-api outlook-restapi outlook-calendar


    【解决方案1】:

    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。然后它会工作。

    【讨论】:

    • 我读到了这个 NPM 包,但他们没有提到如何使用这个 API 传递活动时间和与会者?如果你知道这些事情请告诉我
    • 在您的情况下,您需要使用 jsonBody 而不是 newEvent。然后它会工作。
    • 您能否给我要发布的示例 JSON 正文?
    【解决方案2】:
    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);                         
        }
    });
    

    这是使用 node-outlook 库的示例代码。

    【讨论】:

    【解决方案3】:

    请对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",
      },
     ],
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 2017-10-06
      • 2012-01-15
      • 1970-01-01
      相关资源
      最近更新 更多