1
votes

I am trying to create an application that subscribes to mail events (New mails, Create,update) in office 365 using Microsoft Graph APIs without user interaction. If there is a new email I want to send a notification to my app which has been built using C# MVC5 and .Net 4.5. An example I am following is in Github here (using Daemon Service Github project Link) but it has been built in .Net Core. I came across another application which uses WebJobs and is very close to my requirement
Azure webjobs but it has not been updated in the last 2 years. I followed the steps but most of them do not match with my Azure Developer Account (free). Can some one help me on this please?

App settings :

App Settings:

<add key="tenantId" value="[Tenant_ID]" />
    <add key="clientId" value="[AppID]" />
    <add key="clientSecret" value="[Client Secret]" />
    <add key="webjobs" value="DefaultEndpointsProtocol=https;AccountName=onvidawebjob1aa1b;AccountKey=Z3xewALbEc43gE/dL06HxCGIjVtpaiTkIY4ZolkjZt9Z86xHIZLzMwXBlujNI8/PcTJbM1LIs5KNceVTGgESmA==" />
    <add key="subscriptionblobname" value="subscriptions/subscription1" />
    <add key="notificationurl" value="https://onvidawebjob1.azurewebsites.net/api/HttpTrigger1?code=H7liZOlcQc5qjcZOkRXEb0x4JVqTzoXnrl6mAswsEKl4LlmAYm/ACw==" />

i tried to recreate the app got below issue Trace

Development settings applied
Found the following functions:
WebHooksSample.Functions.ManageSubscriptions
WebHooksSample.Functions.OnNotificationReceived
Singleton lock acquired (cd016b3464fa4d8aa8f41f884a064168/WebHooksSample.Functions.ManageSubscriptions.Listener)
Function 'WebHooksSample.Functions.ManageSubscriptions' is configured to run on startup. Executing now.
Executing 'Functions.ManageSubscriptions' (Reason='Timer fired at 2018-11-16T19:05:27.4182054+05:30', Id=155f7f8a-c352-427f-a7c3-a55ab3518c9a)
Exception while executing function: Functions.ManageSubscriptions
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ManageSubscriptions ---> System.Net.Http.HttpR
equestException: Response status code does not indicate success: 400 (Bad Request).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at WebHooksSample.Functions.<ManageSubscriptions>d__14.MoveNext() in D:\354132\office365_sample_app\webjobs\webjobs-webhooks-sample-master\WebHooks
Sample\Functions.cs:line 128
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`1.<InvokeAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<InvokeAsync>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithWatchersAsync>d__21.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggingAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggingAsync>d__13.MoveNext()
   --- End of inner exception stack trace ---
Exception while executing function: Functions.ManageSubscriptions
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ManageSubscriptions ---> System.Net.Http.HttpR
equestException: Response status code does not indicate success: 400 (Bad Request).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at WebHooksSample.Functions.<ManageSubscriptions>d__14.MoveNext() in D:\354132\office365_sample_app\webjobs\webjobs-webhooks-sample-master\WebHooks
Sample\Functions.cs:line 128
--- End of stack trace from previous location where exception was thrown ---

--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`1.<InvokeAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<InvokeAsync>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithWatchersAsync>d__21.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at `enter code here`System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggingAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggingAsync>d__13.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggingAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<TryExecuteAsync>d__10.MoveNext()
The next 5 occurrences of the schedule will be:
11/16/2018 7:06:03 PM
11/16/2018 7:06:33 PM
11/16/2018 7:07:03 PM
11/16/2018 7:07:33 PM
11/16/2018 7:08:03 PM
Job host started
Executing 'Functions.ManageSubscriptions' (Reason='Timer fired at 2018-11-16T19:05:57.4122054+05:30', Id=405fe97b-2b8a-48bb-a761-7c46a293622c)
Exception while executing function: Functions.ManageSubscriptions
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ManageSubscriptions ---> System.Net.Http.HttpR
equestException: Response status code does not indicate success: 400 (Bad Request).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at WebHooksSample.Functions.<ManageSubscriptions>d__14.MoveNext() in D:\354132\office365_sample_app\webjobs\webjobs-webhooks-sample-master\WebHooks
Sample\Functions.cs:line

EDIT 1:

I was able to accomplish receiving emails (without a user logging in) using outlook connector and office 365 webhook API (using default connector available in azure logic App) similar to what is described here. However, outlook connector polls for every x seconds (custom value set by me) for any change in the mailbox, but I want the service to automatically trigger the Webhook Event without polling. If I understand it correctly, it should be like this -

  1. Create a webhook that is registered to watch for events

  2. Create an outlook connector that uses above webhook.

  3. Outlook connector notifies application (REST API call) as part of workflow.

1
Are you looking for the initial creation of the subscription to be without user interaction, or are you looking for the subsequent notifications to not require user interaction?Philippe Signoret
initial as well as subsequent request it shouldn't ask for credential , because already they have signed with my existing app , if there additional log-in that would annoying the user,Chandru velan
Updated on my side ,somehow i managed to accomplish the above requirement with outlook connector and office 365 webhook API (default connector available in azure logic App) but still outlook connector polls for every x seconds( custom value set by me ) for any change in the mailbox, but i'm looking for without polling it should automatically trigger the Webhook Event. i'm still hoping for any other solutionsChandru velan

1 Answers

1
votes

Here are some key points to consider:

  • Working with Microsoft Graph webhook notifications is the same no matter what language/platform you are using. The only difference is how/where you host your notification endpoint.

  • To subscribe to notifications without user interaction, your app needs app-only permissions

You may want to look into Azure Functions; they provide some easy to work with bindings for Microsoft Graph: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-microsoft-graph