I am referring to o365 API documentation for adding push notification of calendar event. I am getting below error- Post>>
array:4 [▼
"@odata.type" => "#Microsoft.OutlookServices.PushSubscription"
"Resource" => "https://outlook.office.com/api/v2.0/me/events"
"NotificationURL" => "https://mywebsite.com/notifications"
"ChangeType" => "Created,Updated"
]
Response>>
"{"error":{"code":"ErrorInvalidParameter","message":"Notification URL 'https://mywebsite.com/notifications?validationtoken=ODFkNDllYWEtMmExYi00NDVjLWJmNzUtOTBhZjg3MDAyNjhh' verification failed because the response body '\tsuccess\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000' is unexpected."}} ◀"
My code is as-
$post_data = [ "@odata.type"=> "#Microsoft.OutlookServices.PushSubscription",
"Resource"=> "https://outlook.office.com/api/v2.0/me/events",
"NotificationURL"=> 'https://mywebsite.com/notification',
"ChangeType"=> "Created,Updated",
// "expirationDateTime"=> "2018-05-09T18:23:45.9356913Z",
// "clientState"=> "testClientState"
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://outlook.office.com/api/v2.0/me/subscriptions",
//CURLOPT_URL => "https://graph.microsoft.com/beta/subscriptions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($post_data),
CURLOPT_HTTPHEADER => array(
// Set Here Your Requesred Headers
'Authorization: Bearer '.TOKEN,
'Content-Type: application/json',
'X-CSRF-TOKEN: TOKEN-XXXXX'
),
));
How can I resolve the error?
Thanks in advance.
Your notification endpoint must respond to a validation POST in a very specific way. Your response doesn't match that way, so the validation is failing.
From https://developer.microsoft.com/en-us/graph/docs/concepts/webhooks:
Microsoft Graph validates the notification URL in a subscription request before creating the subscription. The validation process occurs as follows:
Microsoft Graph sends a POST request to the notification URL:
POST https://{notificationUrl}?validationToken={TokenDefinedByMicrosoftGraph}
ClientState: {Data sent in ClientState value in subscription request (if any)}
The client must provide a response with the following characteristics within 10 seconds:
The client should discard the validation token after providing it in the response.