Lakhwinder Singh
Lakhwinder Singh

Reputation: 5582

Microsoft Graph api: subscription notification request fail issue

I'm creating subscription for webhooks. For implementation I'm using https://github.com/microsoftgraph/msgraph-sdk-php package.

Below is the implementation of functionality

class WebhookRepository
{

    public function __construct()
    {
        $this->graph = new Graph();
    }

    public function subscribe($accessToken)
    {
        try {

            $this->graph->setAccessToken($accessToken);

            $sub = new Model\Subscription();
            $sub->setChangeType("created,updated");
            $sub->setNotificationUrl(notificationUrl);
            $sub->setResource("/me/mailfolders('inbox')/messages");
            $sub->setClientState('SecretClientState');
            $dateTime = new Carbon();
            $dateTime->addDays(3);
            $sub->setExpirationDateTime($dateTime);

            $subResult = $this->graph->createRequest("POST", "/subscriptions")
                ->attachBody($sub)
                ->setReturnType(Model\Subscription::class)
                ->execute();
        } catch (\Exception $e) {

        }
    }
}

And when I'm executing this request then it send me request to the notificationUrl with validateToken. As mensioned in documentation I'm sending same response as give in step 2

https://learn.microsoft.com/en-us/graph/webhooks#managing-subscriptions

And there is the implementation of notify functionality

public function notify(Request $request)
{
    $token = $request->input('validationToken');
    $response = response()->make($token, 200);
    $response->header('content-type', 'text/plain');
    return $response;
}

and this notify function is returning 200 response code with content type text/plain. with validateToken. But in subscription response I'm getting this error

string(246) "Client error: `POST https://graph.microsoft.com/beta/subscriptions` resulted in a `400 Bad Request` response:
{
  "error": {
    "code": "InvalidRequest",
    "message": "Subscription validation request timed out.",
    "inner (truncated...)
"

I'm stuck here. not getting any example for notify function how to send validation token in request.

FYI: I'm using this in Laravel 5.5 Framework.

Upvotes: 0

Views: 2305

Answers (2)

Daniel Feather
Daniel Feather

Reputation: 1

I had the exact same time out response.

Check to make sure that your request to create the subscription is not blocking the HTTP POST validation request from Microsoft Graph to your notificationUrl.

Microsoft Graph will wait 10 seconds for the notification URL to return a HTTP 200 OK response to verify its active, after that it will deliver a time-out message.

If you're using PHP's built-in web server, it runs only one single-threaded process, so PHP applications will stall if a request is blocked.

https://www.php.net/manual/en/features.commandline.webserver.php

Upvotes: 0

Peter Ciszewski
Peter Ciszewski

Reputation: 647

A naive question: in your validation response, are you actually including the decoded validation token you received in the incoming validation call?

you quoted:

$token = $request->input('validationToken');

is 'validationToken' a placeholder for the actual value (after decoding) that was included in the POST call:

POST https://{notificationUrl}?validationToken={opaqueTokenCreatedByMicrosoftGraph}

Upvotes: 3

Related Questions