我有一个工作的Java程序通过ExchangeWebServices Java类访问Microsoft,现在过期了基本身份验证。
我想要将身份验证方法更改为com.microsoft.aad.msal4j). (包)。
我找到了如何生成ConfidentialClientApplication,然后使用它获取令牌(IAuthenticationResult)的示例。
但是,我不知道如何从OAuth令牌中创建ExchangeCredentials,因为下面的行没有编译,因为ExchangeWebServices中没有类OAuthCredentials
ExchangeCredentials credentials = new OAuthCredentials(authResult.AccessToken); 对于Basic,我可以简单地使用以下代码:
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行之外,在其他地方找到的例子):
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)?
发布于 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
https://stackoverflow.com/questions/74306204
复制相似问题