How to fix Azure Identity => ERROR in getToken()

An Nguyen 0 Reputation points
2023-12-09T08:40:56.63+00:00

I'm trying to use Graph API to use OneDrive but I can't get the token from Interactive Browser.
This is what I get

15:34:27.759 [ForkJoinPool.commonPool-worker-1] ERROR com.microsoft.aad.msal4j.PublicClientApplication -- [Correlation ID: bb141c52-2b89-4ce0-878b-76561b64ce44] Execution of class com.microsoft.aad.msal4j.AcquireTokenByInteractiveFlowSupplier failed. com.microsoft.aad.msal4j.MsalClientException: No Authorization code was returned from the server 	at com.microsoft.aad.msal4j.AcquireTokenByInteractiveFlowSupplier.getAuthorizationResultFromHttpListener(AcquireTokenByInteractiveFlowSupplier.java:225) 	at com.microsoft.aad.msal4j.AcquireTokenByInteractiveFlowSupplier.getAuthorizationResult(AcquireTokenByInteractiveFlowSupplier.java:73) 	at com.microsoft.aad.msal4j.AcquireTokenByInteractiveFlowSupplier.execute(AcquireTokenByInteractiveFlowSupplier.java:46) 	at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:69) 	at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:18) 	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768) 	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1760) 	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373) 	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) 	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) 	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) 	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165) 15:34:27.773 [ForkJoinPool.commonPool-worker-1] ERROR com.azure.identity.InteractiveBrowserCredential -- Azure Identity => ERROR in getToken() call for scopes [api://1db89854-9fd1-4175-93a5-da6f8120819e/Files.Read.All]: Failed to acquire token with Interactive Browser Authentication. Exception in thread "main" com.microsoft.graph.core.ClientException: Error executing the request 	at com.microsoft.graph.http.CoreHttpProvider.sendRequestInternal(CoreHttpProvider.java:410) 	at com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:225) 	at com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:202) 	at com.microsoft.graph.http.BaseRequest.send(BaseRequest.java:335) 	at com.microsoft.graph.requests.DriveRequest.get(DriveRequest.java:56) 	at com.annguyen.getnameandurl.OneDrive.main(OneDrive.java:48) Caused by: java.io.IOException: java.util.concurrent.ExecutionException: com.azure.core.exception.ClientAuthenticationException: Failed to acquire token with Interactive Browser Authentication. 	at com.microsoft.graph.httpcore.AuthenticationHandler.intercept(AuthenticationHandler.java:67) 	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 	at com.microsoft.graph.httpcore.TelemetryHandler.intercept(TelemetryHandler.java:68) 	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 	at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201) 	at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154) 	at com.microsoft.graph.http.CoreHttpProvider.sendRequestInternal(CoreHttpProvider.java:407) 	... 5 more Caused by: java.util.concurrent.ExecutionException: com.azure.core.exception.ClientAuthenticationException: Failed to acquire token with Interactive Browser Authentication. 	at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:396) 	at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2073) 	at com.microsoft.graph.httpcore.AuthenticationHandler.intercept(AuthenticationHandler.java:55) 	... 11 more Caused by: com.azure.core.exception.ClientAuthenticationException: Failed to acquire token with Interactive Browser Authentication. 	at com.azure.identity.implementation.IdentityClient.lambda$authenticateWithBrowserInteraction$52(IdentityClient.java:840) 	at reactor.core.publisher.Mono.lambda$onErrorMap$28(Mono.java:3799) 	at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:94) 	at reactor.core.publisher.MonoFlatMap$FlatMapMain.secondError(MonoFlatMap.java:241) 	at reactor.core.publisher.MonoFlatMap$FlatMapInner.onError(MonoFlatMap.java:315) 	at reactor.core.publisher.MonoCompletionStage$MonoCompletionStageSubscription.apply(MonoCompletionStage.java:115) 	at reactor.core.publisher.MonoCompletionStage$MonoCompletionStageSubscription.apply(MonoCompletionStage.java:67) 	at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934) 	at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:911) 	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) 	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1773) 	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1760) 	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373) 	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) 	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) 	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) 	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165) Caused by: com.microsoft.aad.msal4j.MsalClientException: No Authorization code was returned from the server 	at com.microsoft.aad.msal4j.AcquireTokenByInteractiveFlowSupplier.getAuthorizationResultFromHttpListener(AcquireTokenByInteractiveFlowSupplier.java:225) 	at com.microsoft.aad.msal4j.AcquireTokenByInteractiveFlowSupplier.getAuthorizationResult(AcquireTokenByInteractiveFlowSupplier.java:73) 	at com.microsoft.aad.msal4j.AcquireTokenByInteractiveFlowSupplier.execute(AcquireTokenByInteractiveFlowSupplier.java:46) 	at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:69) 	at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:18) 	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768) 	... 6 more  Process finished with exit code 1 

This is my code

        final String clientId = "";
        final String tenantId = "";
        final String redirectUrl = "http://localhost:8080";
        final List<String> scopes = Arrays.asList("api://{client_id}/Files.Read.All");

        final InteractiveBrowserCredential credential = new InteractiveBrowserCredentialBuilder()
                .clientId(clientId)
                .tenantId(tenantId)
                .redirectUrl(redirectUrl).build();

        final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, credential);

        final GraphServiceClient<Request> graphClient = GraphServiceClient.builder()
                .authenticationProvider(tokenCredentialAuthProvider).buildClient();
        Drive result = graphClient
                .me()
                .drive()
                .buildRequest()
                .get();
        System.out.println("Found Drive " + result.id);
        ```

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

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.