Create Microsoft Graph Client To Access Microsoft Graph API's using MSAL Implementation and OAuth 2 client credential authentication.

The Microsoft Authentication Library (MSAL) helps to acquire tokens from the Microsoft identity platform after authentication.
these security tokens can be used to access Microsoft Graph or Microsoft APIs.

We have used OAuth 2 client credential flow for authentication.









first we have to create app in azure platform and get tenantId, clientId, clientSecret and userPrincipalName.

Class MSAL:

import java.util.Arrays;

import java.util.Collections;

import java.util.concurrent.CompletableFuture;


import com.microsoft.aad.msal4j.ClientCredentialFactory;

import com.microsoft.aad.msal4j.ClientCredentialParameters;

import com.microsoft.aad.msal4j.ConfidentialClientApplication;

import com.microsoft.aad.msal4j.IAuthenticationResult;

import com.microsoft.graph.authentication.IAuthenticationProvider;

import com.microsoft.graph.requests.GraphServiceClient;

import AnyTokenToIAuthenticationProvider;


import okhttp3.Request;


public class MSAL {


// Propertied to configure registered Azure app and user.

private static String tenantId = "***********";

private static String clientId = "***********";

private static String clientSecret = "***********";

private String userPrincipalName = "***********";


private static String authorityUrl = "https://login.microsoftonline.com/";

private static IAuthenticationProvider authprovider = null;

private static GraphServiceClient<Request> graphClient = null;

private static ConfidentialClientApplication confidentialClientApplication;

private static CompletableFuture<IAuthenticationResult> future = null;


public static void main(String[] args) {

IAuthenticationProvider authProvider = getAuthenticationProvider();

graphClient = buildGraphClient(authProvider);

// this graph client can be used to access microsoft graph apis.

}


/**

* Get authentication provider using Client credentials provider(using MSAL access token) through client

* id, secret and tenant id

* @return IAuthenticationProvider

*/

public static IAuthenticationProvider getAuthenticationProvider() {

try {

String authority = authorityUrl + tenantId;

confidentialClientApplication = ConfidentialClientApplication

.builder(clientId, ClientCredentialFactory.createFromSecret(clientSecret)).authority(authority)

.build();

ClientCredentialParameters clientCredentialParam = ClientCredentialParameters

.builder(Collections.singleton(ApplicationConstants.OFFICE_OAUTH_SCOPE)).build();

future = confidentialClientApplication.acquireToken(clientCredentialParam);

IAuthenticationResult IAuthenticationResult = future.get();

authprovider = new AnyTokenToIAuthenticationProvider(IAuthenticationResult.accessToken());

} catch (Exception e) {

e.printStackTrace();

}

return authprovider;


}


/**

* Get graphClient with Authentication provider to access graph API

* @param authProvider

* @return graphClient

*/

public static GraphServiceClient<Request> buildGraphClient(IAuthenticationProvider authProvider) {

try {

graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();

} catch (Exception e) {

e.printStackTrace();

}

return graphClient;

}


}





Class  AnyTokenToIAuthenticationProvider :

import java.net.URL;

import java.util.concurrent.CompletableFuture;


import com.microsoft.graph.authentication.IAuthenticationProvider;


/**

 * 

 * to create graph client we need access token of the type IAuthenticationProvider 

 * this class converts token of any type to IAuthenticationProvider type.

 *

 */

public class AnyTokenToIAuthenticationProvider implements IAuthenticationProvider {

private CompletableFuture<String> accessTokenFuture;

public AnyTokenToIAuthenticationProvider(String accessToken) {

this.accessTokenFuture = new CompletableFuture<>();

this.accessTokenFuture.complete(accessToken);

}


@Override

public CompletableFuture<String> getAuthorizationTokenAsync(URL requestUrl) {

return this.accessTokenFuture;

}

}





Dependencies needed:

<!-- MS Graph libraries -->
<dependency>

    <groupId>com.microsoft.graph</groupId>

    <artifactId>microsoft-graph</artifactId>

    <version>5.27.0</version>

</dependency>


<!-- Microsoft Authentication Library (MSAL) -->

<dependency>

    <groupId>com.microsoft.azure</groupId>

    <artifactId>msal4j</artifactId>

    <version>1.13.0</version>

</dependency>












No comments

darkmode