We are using Microsoft graph sdk for java for the beta api version 0.7.0: https://github.com/microsoftgraph/msgraph-beta-sdk-java
I go through the authentication code flow and in the code that receives the authentication code, we have code like they have in the example here: https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=Java
final AuthorizationCodeCredential authCodeCredential = new AuthorizationCodeCredentialBuilder()
.clientId(clientId)
.clientSecret(clientSecret) //required for web apps, do not set for native apps
.authorizationCode(authorizationCode)
.redirectUrl(redirectUri)
.build();
final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, authCodeCredential);
final GraphServiceClient graphClient =
GraphServiceClient
.builder()
.authenticationProvider(tokenCredentialAuthProvider)
.buildClient();
final User me = graphClient.me().buildRequest().get();
This first call using the graphClient works but subsequent call fails. We steps through some of the classes and in the AuthorizationCodeCredential code to get token:
@Override
public Mono<AccessToken> getToken(TokenRequestContext request) {
return Mono.defer(() -> {
if (cachedToken.get() != null) {
return identityClient.authenticateWithPublicClientCache(request, cachedToken.get())
.onErrorResume(t -> Mono.empty());
} else {
return Mono.empty();
}
}).switchIfEmpty(
Mono.defer(() -> identityClient.authenticateWithAuthorizationCode(request, authCode, redirectUri)))
.map(msalToken -> {
cachedToken.set(new MsalAuthenticationAccount(
new AuthenticationRecord(msalToken.getAuthenticationResult(),
identityClient.getTenantId(), identityClient.getClientId())));
return (AccessToken) msalToken;
})
.doOnNext(token -> LoggingUtil.logTokenSuccess(logger, request))
.doOnError(error -> LoggingUtil.logTokenError(logger, request, error));
}
On the second call using the client there is a cached token but the call to identityClient.authenticateWithPublicClientCache fails with: MsalClientException: Token not found in the cache
Then it falls into the empty case and tries to use the authentication code again which fails because it's already been used from the first call.
We tried setting the tenandId in the calls in case that would help but it did not.
If anyone has some insight into what we need to do to make this work. Please let me know.
Regards, LT