Search code examples
microsoft-graph-apiwebhooksonedriveazure-ad-graph-api

Graph API Webhook subscription for OneDrive not working


I am trying to capture a "document share" event in One Drive using the Graph subscription API. I am able to create a subscription in my Dotnet WebAPI application. Following is my code :

     Microsoft.Graph.User user = funGetO365User("user1@mydomain.onmicrosoft.com");

//Notification  URL = /notification/listen

                Subscription subscription = new Subscription
                {
                    Resource = "/"+ user.Id + "/drive/root",
                    ChangeType = "updated",
                    NotificationUrl = 
 ConfigurationManager.AppSettings["ida:NotificationUrl"],
                    ClientState = Guid.NewGuid().ToString(),
                    //ExpirationDateTime = DateTime.UtcNow + new TimeSpan(0, 0, 4230, 0) // current maximum timespan 
                    ExpirationDateTime = DateTime.UtcNow + new TimeSpan(0, 0, 15, 0) // shorter duration useful for testing
                };

 string contentString = JsonConvert.SerializeObject(subscription, 
                new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, subscriptionsEndpoint);
            request.Content = new StringContent(contentString, System.Text.Encoding.UTF8, "application/json");

 // Send the `POST subscriptions` request and parse the response.
 GraphHttpClient graphHttpClient = new GraphHttpClient(accessToken);
 HttpResponseMessage response = await graphHttpClient.SendAsync(request);

My app is running on localhost and I am using ngrok to relay request to localhost.

I do get Notification once I create a subscription :

[HttpPost]
public async Task<ActionResult> Listen()
{

    // Validate the new subscription by sending the token back to Microsoft Graph.
    // This response is required for each subscription.
    if (Request.QueryString["validationToken"] != null)
    {
        var token = Request.QueryString["validationToken"];

        return Content(token, "plain/text");
    }
    else
    {
    // My code- Never executed
    }
}

However, if I do any changes in the user's one drive,(edit doc, share doc), I am not getting any notification.

Any idea what I need to do to only get notifications?


Solution

  • The issue seems to be resolved . The endpoint URL

    "graph.microsoft.com/v1.0/drives" + user.Id + "/root" 
    

    where user.Id is Graph user unique ID. I am now able to receive the notifications for any changes alright, but was wondering if there is a way that I can get notified only when there is a change in permission of any of drive files?