0

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.

Lakhwinder Singh
  • 5,536
  • 5
  • 27
  • 52

2 Answers2

3

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}

Peter Ciszewski
  • 609
  • 3
  • 6
  • Hi, I'm actually confused how to decode validation token. Could you provide me example how to decode validation token. And what that mean opaqueTokenCreatedByMicrosoftGraph? – Lakhwinder Singh Jan 16 '19 at 05:26
  • 1. opaque token means that you should not parse it, or make assumptions about the shape/format of the token, as it may change in the future. 2. by decoding i meant decoding the URL query string. depending on the libraries you use, this may be done automatically. if you do not decode, the value of the token you are sending us may be encoded and thus fail the comparison check. in general, all query strings must be encoded/decoded when working with web APIs. https://en.wikipedia.org/wiki/Query_string#URL_encoding – Peter Ciszewski Jan 16 '19 at 18:17
0

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