Calendar Events Subscription returns 400

Andrew VT 1 Reputation point
2021-04-15T22:32:05.13+00:00

I am trying to subscribe to calendar events but get a 400 with no reason. Any help would be great. Thanks.

Here is the response:

"Client error: POST https://graph.microsoft.com/v1.0/subscriptions resulted in a 400 Bad Request response: { "error": { "code": "InvalidRequest", "message": "Subscription validation request failed. Response must ex (truncated...) "

Here is my subscription post in PHP

$subscription = [
      "changeType" => "created",
      "notificationUrl" => $notificationUrl,
      // "resource" => "me/calendars/$calendarId/events", // want to do this but not clear if this will work
      "resource" => "me/events",
      "expirationDateTime" => date('c',strtotime('+4 hour +20 minutes')),
      "latestSupportedTlsVersion" => "v1_2"
    ];

$graph = new Graph();
    $graph
      ->setBaseUrl("https://graph.microsoft.com")
      ->setApiVersion("v1.0")
      ->setAccessToken($token['access_token']);

    try {
      $result = $graph->createRequest("POST", "/subscriptions")
              ->addHeaders(array("Content-Type" => "application/json"))
              ->attachBody($subscription)
              ->setTimeout("1000")
              ->execute();
      return json_decode(json_encode($result));
    } catch(Exception $ex){
      error_log(__METHOD__." Error: ".$ex->getMessage());
    }      
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Diana Wanjuhi 1,376 Reputation points
    2021-04-20T06:36:52.1+00:00

    Hello @avantassel to answer your question: "resource" => "me/calendars/$calendarId/events" might not work - from the change notification documentation, subscription is available on an outlook event rather than a calendar. Are you able to reproduce the issue in Postman or Graph Explorer? Perhaps there is a failure parsing the timestamp as sent?

    Let me know if this helps,

    Diana.

    0 comments No comments

  2. Andrew VT 1 Reputation point
    2021-04-20T18:33:00.713+00:00

    Actually just tested this some more and looks like this
    It turns out that the validationToken is coming back as GET and not a POST method as described in the docs.

    https://learn.microsoft.com/en-us/graph/webhooks#notification-endpoint-validation

    I was getting a 400 for not returning the validationToken

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.