0
votes

I'm setting up push notification using the outlook api, the server is written in nodejs. This is the post request the client makes for the subscription

POST /api/v2.0/me/subscriptions HTTP/1.1
Host: outlook.office.com
Content-Type: application/json
client-request-id: f7d3812g-dfbz-4eb5-8edg-0f9a3ad716aq
User-Agent: node-outlook/2.0
return-client-request-id: true
X-Anchor-Mailbox: [email protected]
Authorization: Bearer "ACCESS_TOKEN"
Content-Type: application/json

{
"@odata.type":"#Microsoft.OutlookServices.PushSubscription",
"Resource": "https://outlook.office.com/api/v2.0/me/events",
"NotificationURL": "https://mywebsite.azurewebsites.net/push",  
"ChangeType": "Created,Deleted,Updated"
}

The nodejs server then responds with the validation token that was generated by the outlook notification service

response.writeHead(200, { 'Content-Type': 'text/plain' });
response.write(validation_token);
response.end();

The client (which sent the original post request) then receives the following response

{
 "@odata.context":"https://outlook.office.com/api/v2.0/$metadata#Me/Subscription/$entity",
 "@odata.type": "#Microsoft.OutlookServices.PushSubscription",
 "@odata.id": "https://outlook.office.com/api/v2.0/Users('00034001-ffef-f16e-0000-000000000000@74df9q7f-e9s6-40ad-b43w-aaaaaaaaaaaa')/Subscriptions('RERFNkJFNGUsNEE1My00RjFFLUExQkMtQkU1NkQ9OTdDOTlBXzAwMDM0MDAxLUZGRUYtZTE2RS0wMDAwLTAwMDAwMDAwMEAwMA==')",
 "Id": "RERFNkJFNGUsNEE1My00RjFFLUExQkMtQkU1NkQ9OTdDOTlBXzAwMDM0MDAxLUZGRUYtZTE2RS0wMDAwLTAwMDAwMDAwMEAwMA==",
 "Resource": "https://outlook.office.com/api/v2.0/me/messages",
  "ChangeType": "Created, Updated, Deleted, Missed",
  "NotificationURL": "https://mywebsite.azurewebsites.net/push",
  "SubscriptionExpirationDateTime": "2017-03-30T09:35:37.6586596Z",
  "ClientState": null

}

This is exactly what the outlook documentation states should happen https://dev.outlook.com/restapi/concepts/webhooks

This process validates the NotificationURL to receive push notifications from outlook. The NotificationURL end point is receiving a response from outlook when an event is created, but not the one I want!!!

I should expect to receive something like this:

{
"value": [
    {
        "@odata.type": "#Microsoft.OutlookServices.Notification",
        "Id": null,
        "SubscriptionId": "Mjk3QNERDQQ==",
        "SubscriptionExpirationDateTime": "2015-04-23T22:46:13.8805047Z",
        "SequenceNumber": 1,
        "ChangeType": "Created",
        "Resource" : "https://outlook.office.com/api/v2.0/Users('ddfcd489-628b-7d04-b48b-20075df800e5@1717622f-1d94-c0d4-9d74-f907ad6677b4')/Events('AAMkADNkNmAA=')",
        "ResourceData": {
            "@odata.type": "#Microsoft.OutlookServices.Event",
            "@odata.id": "https://outlook.office.com/api/v2.0/Users('ddfcd489-628b-7d04-b48b-20075df800e5@1717622f-1d94-c0d4-9d74-f907ad6677b4')/Events('AAMkADNkNmAA=')",
            "Id": "AAMkADNkNmAA="
        }
    }
 ]
}

But instead I'm receiving something like this

   {
    _readableState: 
    ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: BufferList { head: null, tail: null, length: 0 },
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: null,
        ended: false,
        endEmitted: false,
        reading: false,
        sync: true,
        needReadable: false,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: true,
        decoder: null,
        encoding: null }, .......

And this response body goes on for another 600 lines.

I know there is a lot going on there, but any tips or help would be greatly appreciated.

1
You should post your code where you're trying to read the received data. What you're showing there looks like a Node object based on a quick search for readableState (github.com/nodejs/node/issues/445). My guess is you're dumping the wrong object.Jason Johnston
@JasonJohnston This is the code: console.log('this is in the push notifications',util.inspect(request, false, null)) response.writeHead(200, { 'Content-Type': 'text/html' }); response.write('Change in inbox'); response.end(); } The push notification data is received at the notification end point, and printed to the screen. I'm not doing any parsing or cleaning of the data just receiving it.Kashif Iqbal

1 Answers

0
votes

I've just found the solution. When I am reading the request from the outlook notification, I have to switch the stream flowing to on, by default this was set to null (see the ReadableState JSON above).

It's very simple, just chain on to the request body, and use a callback. More information can be found here: https://www.sandersdenardi.com/readable-writable-transform-streams-node/

So this becomes

 request.on('data', function(record) {
    console.log('received: ' + record);
});