Outlook呼叫每次呼叫仅返回10个项目

时间:2017-01-17 21:44:01

标签: c# office365 outlook-api

目前正在使用outlook v2.0,这对我来说真的很新,我遇到了一个无法预料的问题。目前我有我的身份验证,并且能够在没有问题的情况下创建我的Outlook客户端。但是,在目前有250封电子邮件的邮箱上进行测试运行之后,我发现这个api只检索了10个。在使用Outlook api v 2.0时查看是否有人遇到过这个问题

代码

    private static async Task<OutlookServicesClient> CreateOutlookClientAsync(AuthenticationContext authenticationContext)
    {

        OutlookServicesClient outlookClient = null;
        try
        {
            outlookClient = new OutlookServicesClient(
                new Uri(CrmPrototype.Helpers.AuthHelper.OutlookAPIEndpoint),
                async () =>
                    await GetTokenHelperAsync(authenticationContext, CrmPrototype.Helpers.AuthHelper.OutlookAuthenticationEndpoint)
                    );                
            return outlookClient;
        }
        catch (Exception ex)
        {
            // TODO Log
            return outlookClient;
        }
    }
    private static async Task<GraphServiceClient> CreateGraphClientAsync(AuthenticationContext authenticationContext)
    {
        GraphServiceClient graphClient = null;
        try
        {
            graphClient = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                async (requestMessage) =>
                {
                    string accessToken = await GetTokenHelperAsync(authenticationContext, CrmPrototype.Helpers.AuthHelper.OutlookAuthenticationEndpoint);

                    // Append the access token to the request.
                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                }));
            return graphClient;
        }
        catch (Exception ex)
        {
            // TODO Log
            return graphClient;
        }
    }
    private static async Task<string> GetTokenHelperAsync(AuthenticationContext context, string resourceId)
    {
        string accessToken = null;
        try
        {
            X509Certificate2 certificate = new X509Certificate2(CrmPrototype.Helpers.AuthHelper.devCertPath, CrmPrototype.Helpers.AuthHelper.devCertKey, X509KeyStorageFlags.MachineKeySet);
            ClientAssertionCertificate clientAssertionCert = new ClientAssertionCertificate(CrmPrototype.Helpers.AuthHelper.devClientId, certificate);                
            AuthenticationResult result = null;
            result = await context.AcquireTokenAsync(resourceId, clientAssertionCert);
            accessToken = result.AccessToken;
            return accessToken;
        }
        catch (Exception ex)
        {
            // TODO Log
            return accessToken;
        }
    }
    public static async Task<IMessageCollection> GetEmails(string emailBox)
    {
        IMessageCollection emails = null;
        AuthenticationContext authenticationContext = new AuthenticationContext(CrmPrototype.Helpers.AuthHelper.devTenant);
        try
        {
            var outlookClient = await CreateOutlookClientAsync(authenticationContext);
            var mail_Box = await outlookClient.Users[emailBox].MailFolders["Inbox"].Messages.OrderByDescending(m => m.ReceivedDateTime).ExecuteAsync();
            var messages = mail_Box.CurrentPage; << only gets 10 emails at a time

            foreach (var message in messages)
            {
                var stop = 0;
            }
            return emails;
        }
        catch (Exception ex)
        {
            // TODO Log
            return emails;
        }
    }   

观看结果

enter image description here

1 个答案:

答案 0 :(得分:1)

名称表示的OutlookREST API是REST API。您正在使用SDK代表您伪造http Web请求。就个人而言,即使我使用C#,我也会手动伪造请求。即使使用SDK,我的建议是你需要查看生成的实际请求并发送到托管api的服务器。我建议你使用像fiddler这样的工具。

话虽如此,$top ODATA参数的默认参数设置为10.这就是为什么你有10个项目,但不要担心你可以通过调用下一个分页来获取它们。您可以在_continuation成员中看到实际的续订网址。请注意值$top$skip参数:这意味着对于此请求,您将获取另外10个项目,跳过10个第一项。

通常,您无法通过一次调用从无限制的来源中获取所有项目。你需要一个分页机制。但是,您可以通过更改$top参数的值来增加页面大小。使用.NET SDK,this page看起来应该使用LINQ中的Take方法。