使用个人帐户时,给定的令牌是EWS OAuth身份验证中的无效错误

发布于 2025-02-13 11:43:47 字数 1805 浏览 1 评论 0原文

我必须从任何帐户中获取Exchange Server的联系人,因此我们从下面的链接中使用了代码。

https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-to-to-authenticate-an-ews-an-ews-application-by-usish-using-oauth

但是它不适用于个人帐户,这对我们的组织帐户工作正常。因此,我已经使用了Aadauthorityaudience属性,而不是tenantid,并将范围从ews.accessasuser.ther更改为其他。现在,身份验证获得了成功,但是在Exchangeservice中使用令牌时,获得“给定令牌无效”错误。

var pcaOptions = new PublicClientApplicationOptions {
    ClientId = "77xxxxxxxxxxx92324",
    //TenantId = "7887xxxxxxxxxxxxx14",
    RedirectUri = "https://login.live.com/oauth20_desktop.srf",
    AadAuthorityAudience = AadAuthorityAudience.AzureAdAndPersonalMicrosoftAccount};

var pca = PublicClientApplicationBuilder.CreateWithApplicationOptions(pcaOptions).Build();

//var ewsScopes = new string[] { "https://outlook.office365.com/EWS.AccessAsUser.All" };
var ewsScopes = new string[] { "User.Read", "Contacts.ReadWrite.Shared" };

var authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();

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

// Make an EWS call
var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));

这里做错了什么?

I have to get the contacts from Exchange server from any account, so we have used the code from below link.

https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth

But it is not working for personal accounts, which is working fine for our organization account. So I have used AadAuthorityAudience property instead of TenantId and changed the scope from EWS.AccessAsUser.All to others. Now authentication got success but getting "The given token is invalid" error while using the token in ExchangeService.

var pcaOptions = new PublicClientApplicationOptions {
    ClientId = "77xxxxxxxxxxx92324",
    //TenantId = "7887xxxxxxxxxxxxx14",
    RedirectUri = "https://login.live.com/oauth20_desktop.srf",
    AadAuthorityAudience = AadAuthorityAudience.AzureAdAndPersonalMicrosoftAccount};

var pca = PublicClientApplicationBuilder.CreateWithApplicationOptions(pcaOptions).Build();

//var ewsScopes = new string[] { "https://outlook.office365.com/EWS.AccessAsUser.All" };
var ewsScopes = new string[] { "User.Read", "Contacts.ReadWrite.Shared" };

var authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();

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

// Make an EWS call
var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));

What am doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2025-02-20 11:43:47

https://outlook.office365.com/ews.accessasuser.all 使用范围。由于不受EWS的支持,因此该范围对个人帐户无效。

https://outlook.office365.com/EWS.AccessAsUser.All is the right scope to use. The scope is invalid for personal accounts since they're not supported by EWS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文