为新电子邮件查询 Office 365 帐户的服务器端任务
Server-side task to query Office 365 account for new emails
我的 .NET 4.6.1/MVC 5 应用程序需要一个服务器端任务,它会定期检查特定的 O365 电子邮件地址是否有新电子邮件,并在找到时检索它们。这似乎是一项非常简单的任务,但我找不到文档 anywhere 来创建服务器端进程来完成此任务。微软似乎拥有的唯一文档是针对 OAuth2 并在用户登录时传递凭据的。我不想要那样。我想检查一个特定的帐户,仅此而已。我将如何做到这一点?
这些是我找到的页面。还有其他的,但都是这样的。
- Intro to the Outlook API - 我看不到将服务帐户与 v2 端点一起使用的方法。
- Get Started with the Outlook REST APIs - 这是特定于使用 OAuth2 登录的用户,对我的目的没有帮助。
Intro to the Outlook API - I don't see a way to use a service account with the v2 endpoint.
v2端点目前不支持客户端凭证(参考limitation). You need to register/configure the app using Azure portal and use the original endpoint to authenticate the app. More detail about register the app please refer to here。我们需要“读取所有邮箱中的邮件”才能使用客户端凭证读取邮件,如下图所示。
下面是使用客户端凭据通过 Microsoft Graph 读取消息的代码:
string clientId = "";
string clientsecret = "";
string tenant = "";
string resourceURL = "https://graph.microsoft.com";
string authority = "https://login.microsoftonline.com/" + tenant + "/oauth2/token";
string userMail = "";
var accessToken = new TokenHelper(authority).AcquireTokenAsync(clientId, clientsecret, resourceURL);
var graphserviceClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
(requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
return Task.FromResult(0);
}));
var items = await graphserviceClient.Users[user].Messages.Request().OrderBy("receivedDateTime desc").GetAsync();
foreach (var item in items)
{
Console.WriteLine(item.Subject);
}
class TokenHelper
{
AuthenticationContext authContext;
public TokenHelper(string authUri)
{
authContext = new AuthenticationContext(authUri);
}
public string AcquireTokenAsync(string clientId, string secret,string resrouceURL)
{
var credential = new ClientCredential(clientId: clientId, clientSecret: secret);
var result = authContext.AcquireTokenAsync(resrouceURL, credential).Result;
return result.AccessToken;
}
}
此外,如果我们使用code grant flow验证应用程序,我们还可以创建一个订阅,当邮箱收到新消息时通知应用程序。(参考webhoocks/subscription)
我的 .NET 4.6.1/MVC 5 应用程序需要一个服务器端任务,它会定期检查特定的 O365 电子邮件地址是否有新电子邮件,并在找到时检索它们。这似乎是一项非常简单的任务,但我找不到文档 anywhere 来创建服务器端进程来完成此任务。微软似乎拥有的唯一文档是针对 OAuth2 并在用户登录时传递凭据的。我不想要那样。我想检查一个特定的帐户,仅此而已。我将如何做到这一点?
这些是我找到的页面。还有其他的,但都是这样的。
- Intro to the Outlook API - 我看不到将服务帐户与 v2 端点一起使用的方法。
- Get Started with the Outlook REST APIs - 这是特定于使用 OAuth2 登录的用户,对我的目的没有帮助。
Intro to the Outlook API - I don't see a way to use a service account with the v2 endpoint.
v2端点目前不支持客户端凭证(参考limitation). You need to register/configure the app using Azure portal and use the original endpoint to authenticate the app. More detail about register the app please refer to here。我们需要“读取所有邮箱中的邮件”才能使用客户端凭证读取邮件,如下图所示。
下面是使用客户端凭据通过 Microsoft Graph 读取消息的代码:
string clientId = "";
string clientsecret = "";
string tenant = "";
string resourceURL = "https://graph.microsoft.com";
string authority = "https://login.microsoftonline.com/" + tenant + "/oauth2/token";
string userMail = "";
var accessToken = new TokenHelper(authority).AcquireTokenAsync(clientId, clientsecret, resourceURL);
var graphserviceClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
(requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
return Task.FromResult(0);
}));
var items = await graphserviceClient.Users[user].Messages.Request().OrderBy("receivedDateTime desc").GetAsync();
foreach (var item in items)
{
Console.WriteLine(item.Subject);
}
class TokenHelper
{
AuthenticationContext authContext;
public TokenHelper(string authUri)
{
authContext = new AuthenticationContext(authUri);
}
public string AcquireTokenAsync(string clientId, string secret,string resrouceURL)
{
var credential = new ClientCredential(clientId: clientId, clientSecret: secret);
var result = authContext.AcquireTokenAsync(resrouceURL, credential).Result;
return result.AccessToken;
}
}
此外,如果我们使用code grant flow验证应用程序,我们还可以创建一个订阅,当邮箱收到新消息时通知应用程序。(参考webhoocks/subscription)