【发布时间】: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