Can someone help how to create graph client using token created by IAuthenticationResult (msal4j)

Vrushabh Shet 1 Reputation point
2022-08-30T07:21:29.717+00:00

Can someone help how to create graph client using token created by IAuthenticationResult (msal4j)

Community Center | Not monitored
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alfredo Revilla - Upwork Top Talent | IAM SWE SWA 27,526 Reputation points Moderator
    2022-09-01T19:38:41.043+00:00

    Hello @Vrushabh Shet and thanks for reaching out. MS Graph clients creation require an instance of IAuthenticationProvider. Several built-in providers and samples are available in Choose a Microsoft Graph authentication provider based on scenario. If you already posses an token you can create your own class/implementation and return the token returned by IAuthenticationResult.getAccessToken() from within the getAuthorizationTokenAsync method.

    E.g.

       // MyAuthenticationProvider.java  
       package com.example;  
       import java.net.URL;  
       import java.util.concurrent.CompletableFuture;  
       import com.microsoft.graph.authentication.IAuthenticationProvider;  
         
       public class MyAuthenticationProvider implements IAuthenticationProvider {  
         
           private CompletableFuture<String> accessTokenFuture;  
         
           public MyAuthenticationProvider(String accessToken) {  
               this.accessTokenFuture = new CompletableFuture<>();  
               this.accessTokenFuture.complete(accessToken);  
           }  
         
           @Override  
           public CompletableFuture<String> getAuthorizationTokenAsync(URL requestUrl) {  
               return this.accessTokenFuture;  
           }  
         
       }  
         
       // App.java  
       package com.example;  
       import com.google.gson.Gson;  
       import com.microsoft.graph.authentication.IAuthenticationProvider;  
       import com.microsoft.graph.models.User;  
       import com.microsoft.graph.requests.GraphServiceClient;  
         
       public class App {  
         
           public static void main(String[] args) {  
         
               String msalAccessToken = args[0];  
         
               final IAuthenticationProvider auth = new MyAuthenticationProvider(msalAccessToken);  
         
               final GraphServiceClient graphClient = GraphServiceClient  
                       .builder()  
                       .authenticationProvider(auth)  
                       .buildClient();  
         
               final User me = graphClient.me().buildRequest().get();  
         
               System.out.println(new Gson().toJson(me));  
         
           }  
       }  
    

    Let us know if you need additional assistance. If the answer was helpful, please accept it and complete the quality survey so that others can find a solution.

    1 person found this answer helpful.

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.