【问题标题】:Getting 403 error when trying to get Folder items from Office 365 mailbox using ExchangeService尝试使用 ExchangeService 从 Office 365 邮箱获取文件夹项目时出现 403 错误
【发布时间】:2022-12-01 03:38:36
【问题描述】:

我正在尝试使用 ExchangeService 从 Office 365 邮箱中读取所有收件箱电子邮件项目。

为此,我:

  • 在我的 AzureAD 门户中创建了一个应用程序。
  • 授予此应用程序所有权限。
  • 向此应用程序颁发访问机密以在我的代码中使用。

该代码的工作原理是我成功拿到token,但是在尝试获取文件夹项目时出现 403 错误:

'请求失败。远程服务器返回错误:(403) 禁止。

我从我的开发环境和我的生产环境中得到这个错误,所以我很确定这不是网络或端口问题。

这是我的代码:

var cca = ConfidentialClientApplicationBuilder
            .Create("myApplicationId")
            .WithClientSecret("myClientSecret")
            .WithTenantId("myTenantId")
            .Build();

var ewsScopes = new string[] { "https://outlook.office365.com/.default" };

// This is where I get the token   
var authResult = await cca.AcquireTokenForClient(ewsScopes).ExecuteAsync();

var ewsClient = new ExchangeService();

ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "my@mail.box");

ewsClient.HttpHeaders.Add("X-AnchorMailbox", "my@mail.box");

// This is where I get the 403 error:
var items = ewsClient.FindItems(
     new FolderId(WellKnownFolderName.Inbox, new Mailbox("my@mail.box")),
     new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter[] {}
     ),
     new ItemView(15)
);

【问题讨论】:

    标签: c# .net office365 exchange-server exchangewebservices


    【解决方案1】:

    403 如果它从 Office365 返回听起来像是他们在您尝试访问的邮箱上禁用了 EWS,或者他们限制了允许连接的客户端,例如 https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-control-access-to-ews-in-exchange。您可以尝试通过 EWSeditor https://github.com/dseph/EwsEditor 使用用户帐户测试 EWS 本身

    【讨论】:

      【解决方案2】:

      我在同一组代码上遇到类似的错误。如果你已经解决了这个问题,你能告诉我吗?同样的解决方案是什么。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 2015-07-19
      • 2021-12-11
      • 2018-04-28
      相关资源
      最近更新 更多