通过Microsoft Graph API订阅消息,无需用户交互

时间:2018-11-15 12:47:23

标签: c# azure-active-directory office365 microsoft-graph azure-webjobs

我正在尝试创建一种无需用户交互即可使用Microsoft Graph API订阅Office 365中的邮件事件(新邮件,创建,更新)的应用程序。如果有新邮件,我想向使用C#MVC5和.Net 4.5构建的应用程序发送通知。我在下面的示例在Github中(使用Daemon Service) Github project Link) 但它已内置于.Net Core中。我遇到了另一个使用WebJobs的应用程序,非常接近我的需求
Azure webjobs,但最近两年未更新。我已按照步骤进行操作,但其中大多数与我的Azure开发人员帐户(免费)不匹配。有人可以帮我吗?

应用程序设置:

应用设置:

<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==" />

我试图重新创建该应用,但出现问题跟踪

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

编辑1:

我能够使用Outlook连接器和Office 365 Webhook API(使用Azure逻辑应用程序中提供的默认连接器)完成与here相似的电子邮件接收(无需用户登录)。但是,Outlook连接器每隔x秒(由我设置的自定义值)轮询一次邮箱中的任何更改,但是我希望该服务自动触发Webhook事件而不进行轮询。如果我正确理解,应该是这样-

  1. 创建一个注册用于监视事件的Webhook

  2. 创建一个使用以上webhook的Outlook连接器。

  3. Outlook连接器在工作流中通知应用程序(REST API调用)。

1 个答案:

答案 0 :(得分:0)

以下是要考虑的一些关键点:

  • 无论使用哪种语言/平台,使用Microsoft Graph Webhook通知都是相同的。唯一的区别是托管通知端点的方式/位置。

  • 要在没有用户交互的情况下订阅通知,您的应用需要仅应用程序权限

您可能想研究Azure函数;它们提供了一些易于使用的Microsoft Graph绑定:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-microsoft-graph