400 Bad request error on using Microsoft Graph API to subscribe to Outlook Push Notifications

落花浮王杯 提交于 2019-12-24 04:06:09

问题


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:


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.

Get the Token:

Response:



来源:https://stackoverflow.com/questions/53661716/400-bad-request-error-on-using-microsoft-graph-api-to-subscribe-to-outlook-push

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!