Trying to import users and groups from Azure Ad programitically through java.

chitra 1 Reputation point
2022-10-04T04:30:44.147+00:00

I using using the Client Credentials to get the access token and then use graphApi to make call to get users profile but running into issue. Not able to understand the actual error for method not found. Most of the example shows similar approach

Added the application registration - using single tenant orgranization.
Created App role to generate the client_id, secret_id and tenant_id.

//Code //
final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId("XXX")
.clientSecret("XXX")
.tenantId("XXXX")
.build();

    List<String> scopes = new ArrayList<>();  
    scopes.add("User.Read.All");  
    final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, clientSecretCredential);  


    GraphServiceClient<Request> graphClient = GraphServiceClient.builder().authenticationProvider(tokenCredentialAuthProvider).buildClient();  

    final User me = graphClient.me().buildRequest().get();  
    assert me != null;  
    System.out.println(me.employeeId);  

SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoSuchMethodError: com.azure.core.credential.TokenRequestContext.getTenantId()Ljava/lang/String;
at com.azure.identity.implementation.util.IdentityUtil.resolveTenantId(IdentityUtil.java:26)
at com.azure.identity.implementation.IdentityClient.lambda$authenticateWithConfidentialClientCache$29(IdentityClient.java:800)
at reactor.core.publisher.Mono.lambda$fromFuture$1(Mono.java:507)
at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:44)
at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:64)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:150)
at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1755)
at reactor.core.publisher.MonoCacheTime$CoordinatorSubscriber.signalCached(MonoCacheTime.java:320)
at reactor.core.publisher.MonoCacheTime$CoordinatorSubscriber.onNext(MonoCacheTime.java:337)
at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2317)
at reactor.core.publisher.MonoCacheTime$CoordinatorSubscriber.onSubscribe(MonoCacheTime.java:276)
at reactor.core.publisher.MonoJust.subscribe(MonoJust.java:54)
at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)
at reactor.core.publisher.MonoCacheTime.subscribeOrReturn(MonoCacheTime.java:132)
at reactor.core.publisher.Mono.subscribe(Mono.java:4203)
at reactor.core.publisher.Mono.subscribeWith(Mono.java:4329)
at reactor.core.publisher.Mono.toFuture(Mono.java:4663)
at com.microsoft.graph.authentication.TokenCredentialAuthProvider.getAuthorizationTokenAsync(TokenCredentialAuthProvider.java:59)
at com.microsoft.graph.httpcore.AuthenticationHandler.intercept(AuthenticationHandler.java:54)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at com.microsoft.graph.httpcore.TelemetryHandler.intercept(TelemetryHandler.java:69)

Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

1 answer

Sort by: Most helpful
  1. Shweta Mathur 30,296 Reputation points Microsoft Employee Moderator
    2022-10-06T05:52:09.81+00:00

    Hi @chitra ,

    Thanks for reaching out.

    I understand you are trying to call Microsoft Graph endpoint to get users and group details using client credential flow and getting the error.

    Client credential flow is for server-to-server interaction where you are running your code without any user context.

    As mentioned, you are calling https://graph.microsoft.com/v1.0/me endpoint which require user context to get user details who logged in to your application(me) which will give error as \me request is only valid with delegated authentication flow.

    To get users details using client credential flow, call https://graph.microsoft.com/v1.0/users/{userId}

    There is Java Azure Sample to call Microsoft graph using client credential flow: https://github.com/Azure-Samples/ms-identity-msal-java-samples/tree/main/1.%20Server-Side%20Scenarios/msal-client-credential-secret

    Hope this will help.

    Thanks,
    Shweta

    ------------------------------

    Please remember to "Accept Answer" if answer helped you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.