I am using the Microsoft Office365 Graph API, and I am needing to set up Webhooks to receive changes to data on email messages and calendar events. I was able to get the Microsoft Graph API working with my PHP Laravel application, but now that I am trying to create webhooks (subscribe to notifications) I am running into issues with validating the "notificationUrl", which is launched to a public server of mine. The script for creating the webhook is returning the following error:
Client error:
POST https://graph.microsoft.com/v1.0/subscriptions
resulted in a400 Bad Request
response: { "error": { "code": "InvalidRequest", "message": "Subscription validation request failed. Response must ex (truncated...)
Which the truncated part I believe is "Subscription validation request failed. Must respond with 200 OK to this request."
Here is my code for creating the webhook subscription:
$data = [
"changeType" => "created",
"notificationUrl" => "https://anatbanielmethod.successengine.net/office365/webhooks/events",
"resource" => "me/events",
"expirationDateTime" => "2018-12-20T18:23:45.9356913Z",
"clientState" => "secret",
];
$result = $graph->createRequest('POST', '/subscriptions')
->attachBody($data)
->execute();
and here is my method for my notificationUrl:
public function events()
{
//if validationToken exists return that to validate notificationUrl
if(isset($_REQUEST['validationToken'])){
return response($_REQUEST['validationToken'], 200)
->header('Content-Type', 'text/plain');
}
//process event normally for those that have already been validated
}
Once again this url is public and live (https://anatbanielmethod.successengine.net/office365/webhooks/events) and I have tested it by using postman to send it test posts and it is working fine, also I added this route to my VerifyCsrfToken middleware to allow a third party post to hit this URL.
Also originally I set up a simple single page PHP script to test validating the notificationUrl and that simple script worked fine, and successfully validates Webhooks created that point to it. Here is that one page script code:
<?php
if(isset($_REQUEST['validationToken'])){
echo $_REQUEST['validationToken']; // needed only once when subscribing
} else {
//process like normal not a validation Token request...
}
}
So I would expect that the Laravel endpoint would work like the simple one page PHP script, and it is when I test both URLs in Postman, but the Laravel endpoint is not validating when Office365 attempts to validate it when creating a new webhook.
I have searched all over for help on this and read through all of the Microsoft Office365 developer documentation I can find on webhooks and these are some of the more helpful parts of the documentation but I am still not finding an answer to this issue:
https://docs.microsoft.com/en-us/graph/api/subscription-post-subscriptions?view=graph-rest-1.0
https://docs.microsoft.com/en-us/graph/webhooks#notification-endpoint-validation
Any ideas of this?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire