c# - 如何在 UWP 应用程序中使用 Microsoft Graph 自动获取新电子邮件

标签 c# uwp microsoft-graph-api

我已使用 OAuth 成功连接到 Microsoft Graph。我可以从我的 Office365 帐户接收和发送电子邮件。

但我完全卡在如何自动接收电子邮件上,类似于 IMAP IDLE 例程。

我在引用

using Microsoft.Graph;
using Microsoft.Toolkit.Services.MicrosoftGraph;

我尝试过订阅,但不知道下一步该做什么,即使这是正确的。

Subscription sub = new Subscription {
    ChangeType = "created",
    NotificationUrl = "urn:ietf:wg:oauth:2.0:oob",
    Resource = "/users/me/messages",
    ExpirationDateTime = DateTimeOffset.Now.AddMinutes(20),
    ClientState = "????" // if applicable, what is this
};

最佳答案

要进行订阅,您需要使用 https 公开通知 URL(请参阅 graph documentation)。

POST https://graph.microsoft.com/v1.0/subscriptions
 Content-Type: application/json
 {
   "changeType": "created,updated",
   "notificationUrl": >  "https://webhook.azurewebsites.net/notificationClient",
   "resource": "/me/mailfolders('inbox')/messages",
   "expirationDateTime": "2016-03-20T11:00:00.0000000Z",
   "clientState": "SecretClientState"
 }

如果你想要例子graph documentation引用 Node.jsasp.net例子。两者都使用 ngrok 来公开 https URL(虽然只是为了测试目的)。该程序通过隧道将 HTTP 请求发送到您的本地主机(如反向代理)。如果您有该设置,则必须验证您的请求。当您发送订阅请求时,您的通知 URL 将收到的第一条发布消息是一 strip 有验证 token 的消息(请参阅 doc )。您必须发回此验证 token 。现在您应该会在指定的通知 URL 上收到通知。

您可以在 graph documentation 中阅读有关订阅资源类型及其属性的信息. 例如,客户端状态描述为:

Specifies the value of the clientState property sent by the service in each notification. The maximum length is 255 characters. The client can check that the notification came from the service by comparing the value of the clientState property sent with the subscription with the value of the clientState property received with each notification.

关于c# - 如何在 UWP 应用程序中使用 Microsoft Graph 自动获取新电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50800116/

相关文章:

c# - 读取并显示C#中读取到的csv文件

c# - 如何从外部类库(通过 NuGet 包)引用 UserControl?

c# - 有没有办法在不测试整个应用程序的情况下对类库(通用 Windows)进行单元测试?

azure - 通过 AAD 进行 SharePoint REST API 调用的访问 token ?

java - 如何在 Spring 中调用外部 API 来测试服务

PocketPC Windows CE 上的 C# 扩展方法

c# - 无法在 .NET 中反序列化 JSON 字符串

c# - asp.net treeview 复选框选择

c# - UWP MediaPlayerElement - 如何循环/重复 mp4

java - ms Graph api(beta) 访问被拒绝