将主题筛选器应用于邮件查询时出现 Office 365 入门项目错误

Office 365 Starter Project error when applying subject filter to mail query

我正在使用 https://github.com/OfficeDev/O365-ASPNETMVC-Start/blob/master/README.md

中的入门项目

我可以运行项目和检索邮件,但是当我修改这个方法时...

internal async Task<List<model.MailItem>> GetEmailMessages(int pageNo, int pageSize)

通过添加下面的 where 子句...

var mailResults = await (from i in outlookServicesClient.Me.Folders.GetById("Inbox").Messages
                                 where i.Subject == "test"
                                 orderby i.DateTimeReceived descending
                                 select i).Skip((pageNo - 1) * pageSize).Take(pageSize).ExecuteAsync();

我收到这个错误...

AdalException This exception is thrown when either you have a stale O365 access token that can cause authentication errors, or you attempted to access a resource that you don't have permissions to access.

我已经启动并重新启动了该项目数次。如果我删除 where 子句,它会再次起作用。如果我重新添加 where 子句,它会再次失败。

通过执行 where 和 orderby,您正在尝试执行复合查询,

GET https://outlook.office365.com/api/v1.0/Me/Folders('Inbox')/Messages?$filter=Subject eq 'Test'&$orderby=DateTimeReceived desc&$skip=0&$top=10

v1.0 outlook server RESTful API 不支持此类复合过滤器。它认为它是一个"InefficientFilter",通过给出这样的错误信息, "The restriction or sort order is too complex for this operation".

您可以通过在客户端进行排序来解决此问题,可能会减少网络流量,如下所示,

var mailResults = await (from i in outlookServicesClient.Me.Folders.GetById("Inbox").Messages
                                 where i.Subject == "test"                                   
                                 select i).Skip((pageNo - 1) * pageSize).Take(pageSize).ExecuteAsync();

var mailMessages = mailResults.CurrentPage.OrderByDescending(i=>i.DateTimeReceived);