I am using the msgraph-sdk-java to read Emails from Exchange Online with a User scope by using UsernamePasswordCredential authorization. I create a client with lazy initialization and then continue to use it, where I had assumed Client would automatically refresh token when it is necessary. Here is the code for client initialization:
private GraphServiceClient<Request> client = null;
private void initializeGraphClient() {
if (this.client == null) {
final UsernamePasswordCredential usernamePasswordCredential = new UsernamePasswordCredentialBuilder().clientId(CLIENT_ID)
.username(user)
.password(password)
.build();
List<String> scopes = Arrays.asList(DEFAULT_SCOPE);
final TokenCredentialAuthProvider tokenCredentialAuthProvider =
new TokenCredentialAuthProvider(scopes, usernamePasswordCredential);
this.client = GraphServiceClient.builder() //
.authenticationProvider(tokenCredentialAuthProvider) //
.buildClient();
}
}
In one of the installations, this works without any problem for a while but fails around 4 Hours later with following problem:
com.microsoft.graph.http.GraphServiceException: Error code: InvalidAuthenticationToken
Error message: Continuous access evaluation resulted in claims challenge with result: InteractionRequired and code: TokenCreatedWithOutdatedPolicies
POST https://graph.microsoft.com/v1.0/me/sendMail
Content-Type : text/plain
SdkVersion : graph-java/v4.0.0
[...]
401 : Unauthorized
[...]
[Some information was truncated for brevity, enable debug logging for more details]
I have seen the latest Version of SDK is 5.x.x, I am using 4.0.0 but I could not see something in change log about refresh problems.
Do I need to refresh token or rebuild client once in a while to avoid such issues?
Also, how can I enable debug logging for Graph SDK Java?