Master Microsoft Graph API with Python: A Comprehensive Tutorial

Logan Jul 13, 2026

Welcome to your comprehensive guide on using Microsoft Graph API with Python. Microsoft Graph API is a RESTful web API that abstracts a set of Microsoft Cloud service resources. By understanding how to use Graph API with Python, you'll be able to interact with Microsoft 365, Windows 10, and Enterprise Mobility + Security. Let's dive into the world of Microsoft Graph API with Python and see what we can create.

🌍✨ PYTHON FOR WORKING WITH APIs: INTEGRATE WITH THE WEB ✨🌍
🌍✨ PYTHON FOR WORKING WITH APIs: INTEGRATE WITH THE WEB ✨🌍

Before we begin, ensure you have the necessary prerequisites. You'll need a Microsoft account, Python 3.6 or later, and the "requests" library installed. If you haven't installed it yet, you can do so with pip: `pip install requests`.

How to plot a graph in python using matplotlib?
How to plot a graph in python using matplotlib?

Setting Up Microsoft Graph API

The first step is to register an application on the Azure Active Directory (Azure AD) portal. This will give us a client ID and client secret which we'll use to authenticate our requests. To register an application:

an image of a computer screen with the text network graph using python
an image of a computer screen with the text network graph using python

1. Go to the Azure portal at https://portal.azure.com/.

Creating a New App Registration

Machine Learning with Python Tutorial
Machine Learning with Python Tutorial

Click on "Azure Active Directory" > "App registrations" > "New registration".

Enter a name, select "Accounts in this organizational directory only", and click "Register".

Obtaining Client ID and Client Secret

Python: Simple Rest API Example and String Formatting
Python: Simple Rest API Example and String Formatting

After the app is registered, note down the "Application (client) ID". This is your client ID.

To get the client secret, go to "Certificates & secrets" > "New client secret". Enter a name, choose a duration, and click "Add". Note down the new secret value; you'll only be able to see it once.

Authenticating with Microsoft Graph API

Python array sorting guide #python #programming #tutorial
Python array sorting guide #python #programming #tutorial

Now that we have a client ID and client secret, we can send authenticated requests to Microsoft Graph API. We'll use the "On-Behalf-Of" flow for this.

First, obtain an access token:

Python Graph Gallery
Python Graph Gallery
Build Python apps with Microsoft Graph - Microsoft Graph
Build Python apps with Microsoft Graph - Microsoft Graph
Build Your First Python API in 6 Steps | Flask Tutorial for Beginners
Build Your First Python API in 6 Steps | Flask Tutorial for Beginners
the top 50 python project ideas
the top 50 python project ideas
a poster with the words string method in python
a poster with the words string method in python
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
Python APIs Explained — Fetch Real Internet Data for Beginners
Python APIs Explained — Fetch Real Internet Data for Beginners
Python Projects Ideas For Beginners
Python Projects Ideas For Beginners
an image of a computer screen with the text'generation ascii art in python '
an image of a computer screen with the text'generation ascii art in python '
draw a stunning tree with Python!
draw a stunning tree with Python!
How to Automate an Excel Sheet in Python?
How to Automate an Excel Sheet in Python?
Python generator expressions cheat sheet #programming #tutorial #python
Python generator expressions cheat sheet #programming #tutorial #python
CHEAT SHEET PYTHON
CHEAT SHEET PYTHON
an image of a computer screen with the text python begin to crashet on it
an image of a computer screen with the text python begin to crashet on it
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
Shortcut to learn Python.[Cheatsheet]
Shortcut to learn Python.[Cheatsheet]
How to Use the String join() Method in Python
How to Use the String join() Method in Python
| Pythonista Planet
| Pythonista Planet
💜 Save this Python String Methods Notes for later!
💜 Save this Python String Methods Notes for later!
Dunder or Magic Methods in Python
Dunder or Magic Methods in Python

Acquiring an Access Token

Send a POST request to `https://login.microsoftonline.com/{tenant_id}/oauth2/token` with the following parameters:

grant_type
client_credentials
client_id
{your_client_id}
client_secret
{your_client_secret}
scope
https://graph.microsoft.com/.default

Including the Access Token in Requests

Once you have the access token, include it in the Authorization header of your requests like this: `Authorization: Bearer {access_token}`.

Making API Requests

Now we're ready to start making API requests. Microsoft Graph API uses standard HTTP methods: GET, POST, PATCH, PUT, DELETE.

For example, to get information about the signed-in user, send a GET request to `https://graph.microsoft.com/v1.0/me`.

Getting User Information

To get user details, send a GET request to `https://graph.microsoft.com/v1.0/users/{user_id}`.

To update user details, send a PATCH request to `https://graph.microsoft.com/v1.0/users/{user_id}` with the new data.

Working with Groups

Microsoft Graph API also allows you to work with groups. To get a list of all groups the signed-in user is a member of, send a GET request to `https://graph.microsoft.com/v1.0/me/joinedGroups`.

To create a new group, send a POST request to `https://graph.microsoft.com/v1.0/groups` with the relevant group details.

And there you have it! You're now equipped with the knowledge to start exploring and interacting with Microsoft Graph API using Python. The possibilities are vast, from managing users and groups to accessing OneDrive files or sending emails using Outlook. Start your journey today and see what you can build!