Outlook 通知 REST API 订阅所有日历
Outlook Notifications REST API Subscribe to all calendars
按照此处的说明进行操作:https://msdn.microsoft.com/en-us/office/office365/api/notify-rest-operations 我得到:
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);
}
}
我把我的代码放在上面是因为我花了一些时间才弄清楚这一切,也许它对某人有帮助。但是,我仍然有一个问题,上面的代码只订阅了默认日历。如果我对其他日历进行更改,则不会发生任何事情。
如何使用 Outlook Notifications REST API 订阅所有日历,而不仅仅是默认日历?
How do I use Outlook Notifications REST API to subscribe to all calendars not only to the default one?
要订阅其他日历的通知,您需要将"Resource"更改为"me/calendars/{calendar_id}/events"。
{
"@odata.type": "#Microsoft.OutlookServices.PushSubscription",
"Resource": "me/calendars/{calendar_id}/events",
"NotificationURL": "...",
"ChangeType": "Created, Updated, Deleted"
}
供大家参考。
按照此处的说明进行操作:https://msdn.microsoft.com/en-us/office/office365/api/notify-rest-operations 我得到:
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);
}
}
我把我的代码放在上面是因为我花了一些时间才弄清楚这一切,也许它对某人有帮助。但是,我仍然有一个问题,上面的代码只订阅了默认日历。如果我对其他日历进行更改,则不会发生任何事情。
如何使用 Outlook Notifications REST API 订阅所有日历,而不仅仅是默认日历?
How do I use Outlook Notifications REST API to subscribe to all calendars not only to the default one?
要订阅其他日历的通知,您需要将"Resource"更改为"me/calendars/{calendar_id}/events"。
{
"@odata.type": "#Microsoft.OutlookServices.PushSubscription",
"Resource": "me/calendars/{calendar_id}/events",
"NotificationURL": "...",
"ChangeType": "Created, Updated, Deleted"
}