What is Microsoft Graph API
Microsoft Graph exposes REST APIs and client libraries to access data on the following Microsoft cloud services:
1 - Microsoft 365 core services: Bookings, Calendar, Delve, Excel, Microsoft 365 compliance eDiscovery, Microsoft Search, OneDrive, OneNote, Outlook/Exchange, People (Outlook contacts), Planner, SharePoint, Teams, To Do, Viva Insights
2 - Enterprise Mobility + Security services: Advanced Threat Analytics, Advanced Threat Protection, Microsoft Entra ID, Identity Manager, and Intune
3 - Windows services: activities, devices, notifications, Universal Print
4 - Dynamics 365 Business Central services
Furthermore, Microsoft Graph API also has subscription services that allows a client app to receive change notifications about changes to data in Microsoft Graph.
For more information on Microsoft Graph API, please refer to the official documentation.
Working with Graph API
As an example, we will be working with the Microsoft Graph API to create a webhook application that listens to changes in the user’s events.
To create subscription, we can either use Microsoft’s msgaraph library or use the requests library.
1 - Microsoft’s msgaraph library
2 - Using the requests library, in settings.py, it stores the client_id, client_secret, tenant_id, scope, and graph_url.
We can see on both of the above examples, there is a parameter called notificationUrl
. This is the URL that Microsoft Graph will send the notification to, and we can obtain it either by setting up GCP cloud run or Azure container apps.
We will dive deeper in the next section.
Building the application on GCP
The setup of the application, on a high level, will be similar to our last blog post on working with cloud run.
The below is the python application structure.
deploy/production/service.yaml
Dockerfile
pyproject.toml
skaffold.yaml
And finally, to deploy our app to GCP Cloud Run.
If you application is successfully deployed, you can see the URL of the application on the GCP console if we navigate to Cloud Run.
The url will look something in the format of https://my-app-xxxx.a.run.app
Building the application on Azure
We will first configure our Azure environment.
1 - Local setup
2- Create required resources in Azure
3 - Set up Azure Container Registry
4 - Login in to Azure Contain Registry Locally
Then we can have a look on our application. The python application will be very similar to the GCP setup. The only difference is the deployment files to Azure Container Apps.
Depending on the Azure VM’s platform, instead of 80, we might want to use 8080 as the port.
For Dockerfile
For skaffold.yaml, it can look something like this.
To deploy our image to Azure Contain Registry
Finally, to deploy our app to Azure Container Apps, we can use the below command.
If you application is successfully deployed, on the terminal where we executed the above command or on the Azure portal, we can find the URL of the application.
The application URL will look something like https://my-app.xxx.centralus.azurecontainerapps.io
If our azure container app needs to reference any secrets, there is 2 ways to do so. One way to do it is to store them as secrets in the container app, and then reference them in the application as environment variable, and the other way is to use the Azure Key Vault.
1 - Storing secrets in the container app and reference them in the application as environment variable.
And we can run the below command in terminal to set the env var, this will only need to execute once.
2 - Azure Key Vault
There are a few steps to set up on Azure to configure permission.
a) We first need to enable Managed Identity for the container app.
To do so, go to the "Settings"
section of our container app on the Azure Portal and select "Identity"
.
In the "System assigned"
tab, switch the "Status"
to "On"
to enable a system-assigned managed identity. Azure will create an identity associated with the Container App and manage its credentials.
Click "Save"
at the top of the panel to apply the changes and we will obtain an object/ principal ID for our system managed identity.
b) Set apporiate permission on the Key Vault.
Go to the "Access policies"
section of the Key Vault and click on "Add Access Policy"
or "Create"
.
Choose the secret permissions you want to grant to the Container App (at minimum, it needs “Get” permission).
Click on "Select principal"
and search for the system-assigned managed identity we created in the previous step. After reviewing the settings, click "Add"
to apply the changes.
c) Reference the secret in the application.
Using the Azure SDK, we can retrieve the secret from the Key Vault.
To reference the secret in the application, we can do the below.
Adding events endpoint to our app
Now, whether we decided to use cloud run or container app, we will have a url that we can use for subscribing the to the data from Microsoft Graph. As a last step, we will want to send the data we received from our MS Graph notification to our Kafka topic.
Below is an example simple implementation.
Afer we have our application set up properly, we will be able to see data from MS Graph start streaming to the kafka topic whenever our subsciprtion receives a change notification.
Example output of the complete json data can be found on the official MS Graph Documentation.
Final Thoughts
The development experiences of the 2 cloud platforms are quite similar. On their Web their, they both of metrics to monitor data volumes and performance, logs are available to debug the application. GCP will provide a deployment YAML template on the UI for user to view and edit, while Azure provides a deployment JSON that we can view on the UI. I’m more familiar with the GCP umbrella, so I found it easier to build the application on GCP. However, I found it interesting to build the architecture on Azure as well, especially the data source is originally from an Azure application.
Thank you for reading and have a nice day!