Is it possible to create webhooks with a personal account to listen to changes in an Excel file using Microsoft Graph?

Oscar González 0 Reputation points
2024-04-03T22:24:34.7066667+00:00

I want to create a webhook to listen for changes in excel files. This is the code I have:

{
     "changeType": "updated",
     "notificationUrl":"xxx.com/notificationClient/",
     "resource":"me/drive/root",
     "expirationDateTime":"2024-04-06T23:09:16Z",
     "clientState": "microsoft_graph"
}

The problem is that I am getting the following error:

"message": "Subscription validation request failed. Response must exactly match validationToken query parameter.",

The code for the notificationUrl page is as follows:

<?php
// Check if the request is a validation request (GET)
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['validationToken'])) {
     // The validation request must respond with the validation token
     $validationToken = $_GET['validationToken'];
     $validationToken = urldecode($validationToken);
     // Set the content type to text/plain
     header('Content-Type: text/plain');
     // Returns the validation token
     echo $validationToken;
     http_response_code(200);
} else if ($_SERVER['REQUEST_METHOD'] === 'POST') {
     // Process the actual change notification (if necessary)
     // Your logic for handling the change notification goes here
     echo 'Notification received successfully.';
     http_response_code(200);
} else {
     // If the request is neither GET nor POST, respond with a 405 status code (Method not allowed)
     http_response_code(405);
}

I wonder if this error is shown because I use a token from a personal Microsoft account or because it occurs?

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
4,364 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,447 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Danstan Onyango 3,816 Reputation points Microsoft Employee
    2024-05-06T11:35:42.1266667+00:00

    The initial request to validate the endpoint consuming the notification is a POST.

    To correct the error, add the logic inside the GET check to the post.

    You will then be sending back the validationToken sent in query but in the POST request not GET.

    0 comments No comments