gpt4 book ai didi

c# - 使用 EWS 和 OAuth 读取邮箱时禁止 403

转载 作者:行者123 更新时间:2023-12-03 03:27:41 33 4
gpt4 key购买 nike

在 Microsoft 弃用 EWS 的基本身份验证后,我正在尝试更新现有应用程序以支持更新类型的身份验证。然而,无论我做什么,通话都会以 403 Forbidden 结束。

如果您能提供任何帮助,我将不胜感激。

代码

   var cca = ConfidentialClientApplicationBuilder
.Create("AppId")
.WithClientSecret("ClientSecret")
.WithTenantId("TenantId")
.Build();

var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
var emailToBeRead = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="285c4d5b5c685c4d5b5c064b4745" rel="noreferrer noopener nofollow">[email protected]</a>";
try
{
var authResult = cca.AcquireTokenForClient(ewsScopes)
.ExecuteAsync().Result;

// Configure the ExchangeService with the access token
var ewsClient = new ExchangeService
{
Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"),
Credentials = new OAuthCredentials(authResult.AccessToken),
ImpersonatedUserId =
new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailToBeRead)
};

//Include x-anchormailbox header
ewsClient.HttpHeaders.Add("X-AnchorMailbox", emailToBeRead);

// Make an EWS call
var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));
foreach (var folder in folders)
{
Console.WriteLine($"Folder: {folder.DisplayName}");
}
}
catch (MsalException ex)
{
Console.WriteLine($"Error acquiring access token: {ex}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex}");
}

权限 Permissions

最佳答案

该问题的解决方案是从 Azure Active Directory 中的 Office 365 Online Exchange API 为将访问邮箱的应用授予 full_access_as_app 权限。

关于c# - 使用 EWS 和 OAuth 读取邮箱时禁止 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75339650/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com