title: Authenticate guest user in Azure AD using graph api
body: <p>I am trying to authenticate users in my web application using Azure AD to store user records. For authenticating the user I am using ADAL4J API (<a href="https://github.com/AzureAD/azure-activedirectory-library-for-java" rel="nofollow noreferrer">https://github.com/AzureAD/azure-activedirectory-library-for-java</a>). I am using the the AuthenticationContext.acquireToken() method to acquire the token for users. This is working for local users in my directory but not for guest users invited to the directory. </p>
  
  <p>While authenticating guest users I am getting an error : "To sign into this application the account must be added to the   directory" . However, I am sure the user has been successfully added to the directory as seen through the Azure Portal. Also, I have verified the same using the graph API where I can see the guest users in the user list in the directory. </p>
  
  <p>So the question is how do I authenticate the guest user in my web application through code (not through redirecting to the Azure UI)?</p>
  
  <p>EDIT : 
  This the method to which I am passing the username and password of the user:</p>
  
  <pre><code> private static AuthenticationResult getAccessTokenFromUserCredentials(
      String username, String password) throws Exception {
      AuthenticationContext context = null;
      AuthenticationResult result = null;
      ExecutorService service = null;
      try {
           service = Executors.newFixedThreadPool(1);
           context = new AuthenticationContext("https://login.windows.net/&lt;tenant_name&gt;", false, service);
           Future&lt;AuthenticationResult&gt; future = context.acquireToken(
              "https://graph.windows.net", CLIENT_ID, username, password,
              null);
           result = future.get();
       } catch(Exception e){
          e.printStackTrace();
       } finally {
           service.shutdown();
       }
  
       if (result == null) {
           throw new ServiceUnavailableException(
                   "authentication result was null");
       }
       return result;
   }
  </code></pre>