Alqin Alqin - 7 months ago 45
PHP Question

Outlook Notifications REST API Subscribe to all calendars

Following the instructions here: https://msdn.microsoft.com/en-us/office/office365/api/notify-rest-operations I got:

subscribe.php

<?php
$access_token=$_SESSION['access_token'];
$user_email=$_SESSION['user_email'];
$headers = array(
"Authorization: Bearer ".$access_token ,
"Accept: application/json",
"X-AnchorMailbox: ".$user_email,
"Content-Type: application/json"
);
$data = '{
"@odata.type":"#Microsoft.OutlookServices.PushSubscription",
"Resource": "https://outlook.office.com/api/v2.0/me/events",
"NotificationURL": "https://mywebsite.com/listener.php",
"ChangeType": "Created, Updated, Deleted",
"ClientState": "secret"
}';
$curl = curl_init('https://outlook.office.com/api/v2.0/me/subscriptions');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
$response = curl_exec($curl);

$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($httpCode >= 400)echo "Request returned status: ".$httpCode;

curl_close($curl);

$json_vals = json_decode($response, true);
echo '<pre>'.print_r($json_vals,true).'</pre>';


listener.php

<?php
if(isset($_REQUEST['validationtoken'])){
echo $_REQUEST['validationtoken']; // needed only once when subscribing
} else {
$headers=getallheaders();
if (isset($headers['ClientState']) && $headers['ClientState'] == "secret"){
$body=json_decode(file_get_contents('php://input'));

file_put_contents(__DIR__.'/listener.text', date("Y-m-d H:i:s").print_r($body,true),FILE_APPEND|LOCK_EX);

}
}


I put my code above because it took me a while to figure it all out and maybe it helps someone. But, I still have an issue, the code above subscribes me only to the default calendar. If I make changes to other calendars nothing happens.

How do I use Outlook Notifications REST API to subscribe to all calendars not only to the default one?

Answer

How do I use Outlook Notifications REST API to subscribe to all calendars not only to the default one?

To subscribe the notification of other calendars, you need to change the the "Resource" to "me/calendars/{calendar_id}/events".

{
  "@odata.type": "#Microsoft.OutlookServices.PushSubscription",
  "Resource": "me/calendars/{calendar_id}/events",
  "NotificationURL": "...",
  "ChangeType": "Created, Updated, Deleted"
}

similar question for your reference.

Comments