title: Microsoft Graph Java SDK and MSAL4J: How to obtain an OnBehalfOf provider to initialize a graph client
body: <p>I'm using Microsoft Graph Java SDK and the MSAL4J Authentication library in order to follow the OnBehalfOf scenario presented here:
  <a href="https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow</a></p>
  <p>Based on the instructions of the Microsoft Graph SDK (can be found here: <a href="https://github.com/microsoftgraph/msgraph-sdk-java" rel="nofollow noreferrer">https://github.com/microsoftgraph/msgraph-sdk-java</a>) I'm able to accomplish the tasks I'm asked to develop.</p>
  <p>But for the authentication provider I have to use MSAL4J, and I'm unable to initialize the graph client because I have a hard time to found how to create an OnBehalfOf auth provider.</p>
  <p>For the authentication part, I'm using the implementation presented in this sample:
  <a href="https://github.com/Azure-Samples/ms-identity-java-webapi" rel="nofollow noreferrer">https://github.com/Azure-Samples/ms-identity-java-webapi</a></p>
  <p>The documentation for Java does not exist yet, and I'm unable to found the solution using the C# documentation:
  <a href="https://docs.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=CS#OnBehalfOfProvider" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=CS#OnBehalfOfProvider</a></p>
  <p>I would like to be able to use what's in place in the ms-identity-java-webapi Azure Sample above in order to create a GraphClient instead of using a RestTemplate to query Graph like this:</p>
  <pre><code>private String callMicrosoftGraphMeEndpoint(String accessToken) {
          RestTemplate restTemplate = new RestTemplate();
  
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON);
  
          headers.set(&quot;Authorization&quot;, &quot;Bearer &quot; + accessToken);
  
          HttpEntity&lt;String&gt; entity = new HttpEntity&lt;&gt;(null, headers);
  
  
          return restTemplate.exchange(&quot;https://graph.microsoft.com/v1.0/me&quot;, HttpMethod.GET,
                  entity, String.class).getBody();
      }
  </code></pre>
  <p>Instead, I would like to obtain an OnBehalfOf auth provider to instantiate a graph client like this:</p>
  <pre><code>GraphServiceClient&lt;Request&gt; graphClient = 
    GraphServiceClient
      .builder()
      .authenticationProvider(authenticationProvider)
      .buildClient();
  </code></pre>
  <p>This kind of auth provider can be obtained in C# like this, but I don't found how to do it in java:</p>
  <pre><code>IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
      .Create(clientId)
      .WithRedirectUri(redirectUri)
      .WithClientSecret(clientSecret)
      .Build();
  
  OnBehalfOfProvider authProvider = new OnBehalfOfProvider(confidentialClientApplication, scopes);
  </code></pre>
  <p>A similar issue has already been raised here:
  <a href="https://github.com/Azure-Samples/ms-identity-java-webapi/issues/19" rel="nofollow noreferrer">https://github.com/Azure-Samples/ms-identity-java-webapi/issues/19</a></p>
  <p>But sadly, Microsoft haven't updated anything yet, and I don't seem to figure out how to achieve this task as I'm totally new to this library and have only handled the Graph SDK through the available documentation and samples (which does not seem to implement MSAL for now)</p>