首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过ExchangeWebServices Java类和OAuth对Microsoft进行身份验证?

如何通过ExchangeWebServices Java类和OAuth对Microsoft进行身份验证?
EN

Stack Overflow用户
提问于 2022-11-03 16:09:06
回答 1查看 88关注 0票数 -1

我有一个工作的Java程序通过ExchangeWebServices Java类访问Microsoft,现在过期了基本身份验证

我想要将身份验证方法更改为com.microsoft.aad.msal4j). (包)。

我找到了如何生成ConfidentialClientApplication,然后使用它获取令牌(IAuthenticationResult)的示例。

但是,我不知道如何从OAuth令牌中创建ExchangeCredentials,因为下面的行没有编译,因为ExchangeWebServices中没有类OAuthCredentials

代码语言:javascript
复制
ExchangeCredentials credentials = new OAuthCredentials(authResult.AccessToken); 

对于Basic,我可以简单地使用以下代码:

代码语言:javascript
复制
ExchangeService     service     = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ExchangeCredentials credentials = new WebCredentials(<user>,<password>);
service.setCredentials(credentials);
service.setUrl(new URI(<Exchange_URI>));

现在,对于oauth的msal4j,我开始如下(除了最后2行之外,在其他地方找到的例子):

代码语言:javascript
复制
IClientCredential credential = ClientCredentialFactory.createFromSecret(context.Exchange_AzureClientSecret);
ConfidentialClientApplication cca =
        ConfidentialClientApplication
                .builder(<Exchange_AzureClientId>, credential)
                .authority(<Exchange_Authority>)
                .build();
IAuthenticationResult authResult;
Set<String> scope = Collections.singleton("https://outlook.office365.com/.default");
     try {
         SilentParameters silentParameters =
                 SilentParameters
                         .builder(scope)
                         .build();
         // try to acquire token silently. This call will fail since the token cache does not
         // have a token for the application you are requesting an access token for
         authResult = cca.acquireTokenSilently(silentParameters).join();
     } catch (Exception ex) {
         if (ex.getCause() instanceof MsalException) {
             ClientCredentialParameters parameters =
                     ClientCredentialParameters
                             .builder(scope)
                             .build();
             // Try to acquire a token. If successful, you should see
             // the token information printed out to console
             authResult = cca.acquireToken(parameters).join();
         } else {
             // Handle other exceptions accordingly
             throw ex;
         }
     }
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ExchangeCredentials credentials = new OAuthCredentials(authResult.AccessToken);
//...

如何从ExchangeCredentials令牌中创建OAuth (此处命名为authResult)?

EN

回答 1

Stack Overflow用户

发布于 2022-11-03 22:54:44

官方Java托管API不再由微软更新或维护,也不支持oAuth。它的开放源码和一些人已经分叉了它,并在https://github.com/OfficeDev/ews-java-api/issues?q=is%3Aissue+is%3Aopen+oauth中添加了这种支持。

https://github.com/bookaroom/RESTApi/blob/master/src/com/comeet/exchange/BearerTokenCredentials.java

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74306204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档