无法使用C#中的exchange服务访问共享office 365邮箱

问题描述 投票:0回答:1

我正在尝试读取通过 Microsoft365 管理的我的组织的共享邮箱。 但我收到错误消息:Microsoft.Exchange.WebServices.Data.ServiceRequestException:'请求失败。远程服务器返回错误:(403) Forbidden。'

代码如下:

    `IConfidentialClientApplication cca = ConfidentialClientApplicationBuilder
    .Create(ConfigurationManager.AppSettings["appId"])
    .WithClientSecret(ConfigurationManager.AppSettings["clientSecret"])
    .WithTenantId(ConfigurationManager.AppSettings["tenantId"])
    .Build();

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

    AuthenticationResult authResult = await cca.AcquireTokenForClient(ewsScopes).ExecuteAsync();
    string accessToken = authResult.AccessToken;

    if (accessToken != null)
    {

        var ewsClient = new ExchangeService();
        ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
        ewsClient.Credentials = new OAuthCredentials(accessToken);
        ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.PrincipalName, "[email protected]");

        ewsClient.HttpHeaders.Add("X-AnchorMailbox", "[email protected]");

        var folders = ewsClient.FindFolders(WellKnownFolderName.Inbox, new FolderView(10));
    }`

我也尝试添加这段代码:

FolderId SharedMailbox = new FolderId(WellKnownFolderName.Inbox,"[email protected]"); ItemView itemView = new ItemView(1000); service.FindItems(SharedMailbox,itemView);

我在最后一行调用 FindFolders 方法时出现上述错误。采取什么步骤来解决这个问题?

c# office365 exchange-server exchangewebservices
1个回答
0
投票

您的 EWS 代码是否适用于其他邮箱,例如您能否获得您使用的邮箱的收件箱

ewsClient.HttpHeaders.Add("X-AnchorMailbox", "[email protected]");

A 403 可能意味着很多事情首先是可以在该邮箱上禁用 EWS 或通过应用程序策略参见https://practical365.com/new-application-access-policies-extend-support-for-more-场景/。您可以使用 EWSEditor 针对特定邮箱测试 EWS 的可用性https://github.com/dseph/EwsEditor.

要做的另一件事是检查您是否使用 jwt.io 之类的东西在访问令牌中获得了正确的权限

应该看起来像

© www.soinside.com 2019 - 2024. All rights reserved.