按照此处的说明进行操作: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通知REST API订阅所有日历,而不仅仅订阅默认日历
如何使用Outlook通知REST API订阅所有日历,而不仅仅订阅默认日历?
要订阅其他日历的通知,您需要将"资源"更改为"我/日历/{calendar_id}/事件"。
{
"@odata.type": "#Microsoft.OutlookServices.PushSubscription",
"Resource": "me/calendars/{calendar_id}/events",
"NotificationURL": "...",
"ChangeType": "Created, Updated, Deleted"
}
类似的问题供您参考。