Subscribe for Microsoft Graph change notifications to be notified when your user's data changes, so you don't have to poll for changes.
This sample Java Spring web application shows how to subscribe for change notifications to be delivered to Azure Event Grid.
This sample uses:
- The Microsoft Graph SDK for Java (SDK) to call Microsoft Graph.
- The Azure Identity client library for Java library to handle authentication for Microsoft Graph.
- Java 17 or later or later.
- The .NET dev tunnel CLI.
- A Microsoft work or school account with the Global administrator role. If you don't have one, you can get a developer sandbox by joining the Microsoft 365 developer program.
- An Azure subscription. If you don't have one, you can start a free trial.
In order to configure the sample, you'll need to do the following steps in the Azure portal.
- Register an application for authenticating to Microsoft Graph.
- Create a resource group where Microsoft Graph can create partner topics for Event Grid.
- Authorize Microsoft Graph to create partner topics in the resource group.
-
Open a browser and navigate to the Azure Active Directory admin center and login using a Global administrator account.
-
Select Microsoft Entra ID in the left-hand navigation, then select App registrations under Manage.
-
Select New registration. Enter a name for your application, for example,
Event Grid Change Notifications Sample
. -
Set Supported account types to Accounts in this organizational directory only.
-
Leave Redirect URI empty.
-
Select Register. On the application's Overview page, copy the value of the Application (client) ID and Directory (tenant) ID and save them, you will need these values in the next step.
-
Select API permissions under Manage.
-
Remove the default User.Read permission under Configured permissions by selecting the ellipses (...) in its row and selecting Remove permission.
-
Select Add a permission, then Microsoft Graph.
-
Select Application permissions.
-
Select User.Read.All, then select Add permissions.
-
Select Grant admin consent for..., then select Yes to provide admin consent for the selected permission.
-
Select Certificates and secrets under Manage, then select New client secret.
-
Enter a description, choose a duration, and select Add.
-
Copy the secret from the Value column, you will need it in the next steps.
[!IMPORTANT] This client secret is never shown again, so make sure you copy it now.
-
Open a browser and navigate to the Azure Active Directory admin center and login using a Global administrator account.
-
Select Resource groups in the left-hand navigation, then select Create.
-
Select the Azure subscription and region to create the resource group in, and provide a name for the resource group, then select Review + create.
-
Once the validation completes, select Create.
-
Open a browser and navigate to the Azure Active Directory admin center and login using a Global administrator account.
-
Search for Event Grid Partner Configurations and select it from the results.
-
Select Create.
-
Select the Azure subscription and the resource group you created in the previous step.
-
Select Partner Authorization.
-
Search for MicrosoftGraphAPI and select it. Select Add.
-
Select Review + create. Once the validation completes, select Create.
Create a new file named application.yml in the ./grapheventgrid/src/main/resources directory and add the following.
azure:
tenant-id: YOUR_TENANT_ID
client-id: YOUR_CLIENT_ID
client-secret: 'YOUR_CLIENT_SECRET'
subscription-id: YOUR_AZURE_SUBSCRIPTION_ID
resource-group: YOUR_EVENT_GRID_RESOURCE_GROUP
event-grid-topic: YOUR_EVENT_GRID_TOPIC
location: YOUR_AZURE_LOCATION
Set the values as follows.
Setting | Value |
---|---|
tenant-id |
The Directory (tenant) ID from your app registration. |
client-id |
The Application (client) ID from your app registration. |
client-secret |
The client secret from your app registration. |
subscription-id |
The ID of your Azure subscription. This can be found in the Azure portal. Search for Subscriptions and select it from the results. |
resource-group |
The name of the resource group you created in the previous steps. |
event-grid-topic |
The name Microsoft Graph should use to create the partner topic, for example: EventGridNotifications . |
location |
The location you created your resource group in. You can find this by running the following command in Cloud Shell in the Azure portal: az account list-locations . Use the name value for the location, for example: eastus . |
A dev tunnel will allow Azure Event Grid to reach the sample running on your development machine.
-
Run the following command to login to the dev tunnel service. You can login with either a Microsoft Azure Active Directory account, a Microsoft account, or a GitHub account.
devtunnel user login
-
Run the following commands to create a tunnel. Copy the Tunnel ID from the output.
devtunnel create --allow-anonymous
-
Run the following command to assign the sample's port (8080) to the tunnel. Replace
tunnel-id
with the Tunnel ID copied in the previous step.devtunnel port create tunnel-id -p 8080
-
Run the following command to host the tunnel. Replace
tunnel-id
with the Tunnel ID copied in the previous step.devtunnel host tunnel-id
-
Copy the URL labeled Connect via browser. Open this URL in your browser and select Continue to enable the tunnel.
-
Run the sample with the following command in the grapheventgrid directory. Alternatively, you can press F5 in Visual Studio Code.
gradlew run bootRun
-
Monitor the output. When you see the following, proceed to the next step.
Created new subscription with ID 4264cf0b-1c5d-4ab0-8097-cc96b894634a Please activate the GraphUserNotifications partner topic in the Azure portal and create an event subscription. See README for details.
-
In the Azure portal, navigate to the resource group you created. It should contain a new Event Grid Partner Topic. Select this topic.
-
Select Activate.
-
Select Event Subscription to add a new subscription.
-
Provide a name for the subscription, then set Endpoint Type to Webhook.
-
Select Configure an endpoint. Enter your dev tunnel URL. Select Confirm Selection.
-
Select Create and wait for the deployment to succeed.
Using the Azure portal or the Microsoft admin center, add, update, or delete users. Watch the sample's output for the notifications.
Received Microsoft.Graph.UserUpdated notification from Event Grid
User Adele Vance (ID: 37a7dc1b-812b-4336-8210-76b2180b574a) was created or updated
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.