Calling the Azure AD Graph API in a web application

게시자: Brandon Werner
마지막 업데이트: 2017-06-30
GitHub에서 편집

This Java sample application is a web app that reads directory data from the Azure Active Directory Graph API, and executes queries against a demo company. The full list of capabilities for the Graph API can be found on MSDN (link below). If you would like to access your own Azure AD tenant's directory data, then the application must be configured with your own Tenant ID, Application ID and App Key - these values are stored in the app's web.xml file, then rebuild the application.

How To Run This Sample

Getting started is simple! To run this sample you will need: - JDK 7 Standard Edition (JDK7 or greater) - Eclipse IDE - Apache Tomcat - An Internet connection - An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, please see How to get an Azure AD tenant - A user account in your Azure AD tenant. This sample will not work with a Microsoft account, so if you signed in to the Azure portal with a Microsoft account and have never created a user account in your directory before, you need to do that now.

Step 1: Clone or download this repository

From your shell or command line:

git clone

Step 2: Register the sample with your Azure Active Directory tenant

  1. Sign in to the Azure portal.
  2. On the top bar, click on your account and under the Directory list, choose the Active Directory tenant where you wish to register your application.
  3. Click on More Services in the left hand nav, and choose Azure Active Directory.
  4. Click on App registrations and choose Add.
  5. Enter a friendly name for the application, for example 'WebApp-GraphAPI-Java' and select 'Web Application and/or Web API' as the Application Type. For the sign-on URL, enter the base URL for the sample, which is by default https://localhost:44320/. Click on Create to create the application.
  6. While still in the Azure portal, choose your application, click on Settings and choose Properties.
  7. Find the Application ID value and copy it to the clipboard.
  8. For the App ID URI, enter https://<your_tenant_name>/WebApp, replacing <your_tenant_name> with the name of your Azure AD tenant.
  9. From the Settings menu, choose Keys and add a key - select a key duration of either 1 year or 2 years. When you save this page, the key value will be displayed, copy and save the value in a safe location - you will need this key later to configure the project in Visual Studio - this key value will not be displayed again, nor retrievable by any other means, so please record it as soon as it is visible from the Azure Portal.
  10. Configure Permissions for your application - in the Settings menu, choose the 'Required permissions' section, click on Add, then Select an API, and select 'Microsoft Graph' (this is the Graph API). Then, click on Select Permissions and select 'Read Directory Data'.

Step 3: Configure the sample to use your Azure Active Directory tenant

The Sample application can be built using the Eclipse IDE, and runs under Tomcat.The following instructions are provided:

1.Download and install the JDK 7 from the Oracle Website (select the version for your development environment (e.g. Windows x64) JDK 7

  1. Select JDK Standard Edition (JDK SE 7 with latest update).

  2. Set a system environmental variable named JAVA_HOME and give the variable value to your java installation. Typically, this value would be something like: C:\Program Files\Java\jdk1.7.0_06

  3. Download and install the Eclipse IDE for Java EE Developers from the following website (select the version for your development environment (e.g. Windows x86). Eclipse IDE for Java EE - simply execute Eclipse.exe from a command line.

  4. Download and install Apache Tomcat. Apache TomCat ( You can run Apache Tomcat by clicking /bin/startup.bat script. You can also shut down apache tomcat by clicking mcat folder>/bin/shutdown.bat. When running your application from Eclipse, it can be configured to automatically start Apache Tomcat, and execute in normal or debug mode.

  5. Import the project in Eclipse by: a. Starting Eclipse by clicking Eclipse.exe. b. Specify a workspace folder of your choice. c. Click File/Import/General/Existing Projects Into the workspace d. Select the root directory to the project you want to import into. e. Click Finish.

Optionally, you can also import the JavaSampleApp.war file by selecting from Eclispe: File/Import/Web/War and selecting the JavaSampleApp.war file.

  1. To see the opened project: a. Click Window/Show View/Project Explorer. This would show the project explorer which would show the whole project hierarchy on the left of your screen.

b. update the web.xml file - under WebContent -> WEB-INF, locate web.xml file and open it - update three init-param values in the web.xml: i. Replace the current param-value under TenantContextId with your tenant domain name (e.g. Also replace the param-value under TenantDomainName with your tenant domain name. ii. Replace the param-value under AppPrincipalId with the client ID value that was configured from the Azure Management Portal. iii. Replace the param-value under SymmetricKey with the Key value that was configured from the Azure Managment Portal. Save the web.xml file. c. Now right click on the project name, and select ?Run As/Run on Server?. d. This would prompt you to specify a server
i. Select Apache Tomcat V7.0
ii. Select the root directory of the tomcat server.
iii. Select ?Always use this server?.

About The Code

Coming Soon.