Reputation: 41
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?
Upvotes: 3
Views: 2714
Reputation: 13739
One thing that for some reason is not mentioned in the subscriptions article, but mentioned in another article is that notificationUrl
must return the validation token in plain text. If you don't return the validation token, a misleading "The request is invalid due to validation error" without any details will be returned on attempt to subscribe.
Here is what graphapi sends in URL Search parameters to your notificationUrl
:
validationToken=Validation:+Testing+client+application+reachability+for+subscription+Request-Id:+12c10ed5-4081-4826-ab8d-782819d1b32a
You are to echo the value of the validationToken
as plain text in the response body for validation to pass.
Upvotes: 0