2
votes

I am trying to create a subscription to get Outlook emails @mentions via Push Notification using Microsoft Graph API. I am using this documentation for reference/

I created an app in portal.azure.com and provided Required permission on "Read user mail" under "Microsoft Graph" API and admin consented it through Microsoft Demo tenant. But I am still not able to subscribe to the Microsoft Graph Push Notification REST API and I am getting 400 Bad Request error.

I also tried granting all permissions to the app in portal.azure.com under the Microsoft Graph API and again admin consented it through my Microsoft demo tenant but I am still getting same 400 bad request error.

I am using the following code to create a subscription:

private static void Subscribe(AuthenticationHeaderValue authHeader) 
{
    using(HttpClient _httpClient = new HttpClient()) 
    {
       _httpClient.BaseAddress = new Uri("https://graph.microsoft.com");
       _httpClient.DefaultRequestHeaders.Accept.Clear();
       _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
       _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
       _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
       _httpClient.DefaultRequestHeaders.Authorization = authHeader;

       // connect to the REST endpoint to subscribe   
       var json = @"{
                       'changetype':'created,updated',
                       'notificationurl': 'http://requestbin.fullcontact.com/two54stw/',
                       'resource': 'me/mailfolder(\'Inbox\')/messages',
                       'clientstate': 'secretclientvalue'
                   }";

       HttpContent contentPost = new StringContent(json.ToString(), Encoding.UTF8, "application/json");
       HttpResponseMessage subscribe = _httpClient.PostAsync($"v1.0/subscriptions", contentPost).Result;
       Console.WriteLine("New subscription created!");
    }
}

Has anyone worked on a similar issue to create a Push notification Subscription to get Outlook emails and can please help?

1
What is the actual error you're getting back? There is more than just the 400 status code. Also, what is the actual value of authHeader? - Marc LaFleur
@Neha Hi,does my answer helps you> - Jay Gong

1 Answers

1
votes

Neha.Based on the 400 Bad Request error, I assume that the notificationurl is not configured correctly.

Please refer to this article and I followed the sample from here.It works for me.

enter image description here

Get the Token:

enter image description here

Response:

enter image description here