'Java MS Graph SDK get GraphClient Using an Existing AccessToken (5.13.0 version)

Our Front End using PKCE flow and fetches a access Token. As per the old implementation ([email protected] version) this below snippet gets a Graph Client using an existing access token. Now I cannot get the same working in the newer MS Graph Java SDK.

IGraphServiceClient client = GraphServiceClient.builder()
                        .authenticationProvider( request -> request.addHeader("Authorization", "Bearer " + tokenAuthentication.getToken().getTokenValue()) )
                        .buildClient(); 

Dependencies I have added to my project

<dependency>
            <!-- Include the sdk as a dependency -->
            <groupId>com.microsoft.graph</groupId>
            <artifactId>microsoft-graph</artifactId>
            <version>5.13.0</version>
        </dependency>
        <dependency>
            <!-- This dependency is only needed if you are using the TokenCrendentialAuthProvider -->
            <groupId>com.azure</groupId>
            <artifactId>azure-identity</artifactId>
            <version>1.2.5</version>
        </dependency>


Solution 1:[1]

finally got it working.. see below snippet..

IAuthenticationProvider authProvider = new IAuthenticationProvider() {
            
            @Override
            public CompletableFuture<String> getAuthorizationTokenAsync(URL requestUrl) {
                CompletableFuture<String> future = new CompletableFuture<>();
                future.complete(yourToken);
                return future;
            }
        };
        
        GraphServiceClient<Request> graphClient = GraphServiceClient
                .builder()
                .authenticationProvider(authProvider)
                .buildClient();

         return graphClient.me().buildRequest().get();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Henry Gengiti