Outlook通知REST API订阅所有日历

时间:2016-04-18 22:39:39

标签: php office365 office365api outlook-restapi

按照这里的说明: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订阅所有日历,而不仅仅是默认日历?

1 个答案:

答案 0 :(得分:2)

  

如何使用Outlook Notifications REST API订阅所有日历,而不仅仅是默认日历?

要订阅其他日历的通知,您需要将“资源”更改为“me / calendars / {calendar_id} / events”。

// When Available button is click -> changes whole calendar to available
$("#yes").on("click", function() {
    $(".calCell:not(.empty)").css('background-color', 'green');
});

// When Unavailable button is clicked -> changes whole calendar to unavailable
$("#no").on("click", function() {
    $(".calCell:not(.empty)").css('background-color', 'red');
});

similar question供您参考。