Microsoft Graph API return 401 Unauthorized -"Identity required"
Hello! I am currently integrating a Spring Boot application with the Microsoft Graph API. In my application I am using the Microsoft Graph SDK.
Hello! I am currently integrating a Spring Boot application with the Microsoft Graph API. In my application I am using the Microsoft Graph SDK.
My problem is that on some calls to the Graph API, it returns 401 Unauthorized to me, but a subsequent request with the same Bearer token will return 200.
I have extracted the token and tried the same thing through Postman and the result was the same - the Graph API would return 401 on some occasions. This seems rather random to me and I could not find any reason behind it.
It should not be a matter of token expiration or scopes.
The problem occurs only when I make a call to https://graph.microsoft.com/v1.0/teams/{teamId}/channels
graphServiceClient
.teams(teamId)
.channels()
.buildRequest()
.get();
Here is the full error response body that the API returns to me:
{
"error": {
"code": "Unauthorized",
"message": "Identity required",
"innerError": {
"message": "Identity required",
"code": "NoAuthenticationToken",
"innerError": {},
"date": "2023-07-27T07:13:32",
"request-id": "UUID",
"client-request-id": "UUID"
}
}
}
And here is the Bean configuration I have in my application:
@Bean
public GraphServiceClient<Request> graphServiceClient() {
final UsernamePasswordCredential credential = new UsernamePasswordCredentialBuilder()
.clientId(clientId)
.tenantId(tenantId)
.username(systemUserUsername)
.password(systemUserPassword)
.build();
List<String> scopes = Arrays.stream(systemUserScopes).toList();
final TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(scopes, credential);
return GraphServiceClient.builder()
.authenticationProvider(authProvider)
.buildClient();}