Microsoft Graph sample Python web app
This sample uses Microsoft Graph to access data in Office 365 by building a Python Django web application using the Azure AD v2 authentication endpoint.
Prerequisites
- Python (with pip) installed on your development machine. If you do not have Python, visit the previous link for download options. (Note: This tutorial was written with Python version 3.7.9 and Django version 3.2.15 The steps in this guide may work with other versions, but that has not been tested.)
- Either a personal Microsoft account with a mailbox on Outlook.com, or a Microsoft work or school account.
If you don't have a Microsoft account, there are a couple of options to get a free account:
- You can sign up for a new personal Microsoft account.
- You can sign up for the Microsoft 365 Developer Program to get a free Office 365 subscription.
Register a web application with the Azure Active Directory admin center
Open a browser and navigate to the Azure Active Directory admin center. Login using a personal account (aka: Microsoft Account) or Work or School Account.
Select Azure Active Directory in the left-hand navigation, then select App registrations under Manage.
Select New registration. On the Register an application page, set the values as follows.
- Set Name to
Python Graph Tutorial. - Set Supported account types to Accounts in any organizational directory and personal Microsoft accounts.
- Under Redirect URI, set the first drop-down to
Weband set the value tohttp://localhost:8000/callback.
- Set Name to
Choose Register. On the Python Graph Tutorial page, copy the value of the Application (client) ID and save it, you will need it in the next step.
Select Certificates & secrets under Manage. Select the New client secret button. Enter a value in Description and select one of the options for Expires and choose Add.
Copy the client secret value before you leave this page. You will need it in the next step.
Important
This client secret is never shown again, so make sure you copy it now.
Configure the sample
Rename the
oauth_settings.example.ymlfile tooauth_settings.yml.Edit the
oauth_settings.ymlfile and make the following changes.- Replace
YOUR_APP_ID_HEREwith the Application Id you got from the App Registration Portal. - Replace
YOUR_APP_PASSWORD_HEREwith the password you got from the App Registration Portal.
- Replace
In your command-line interface (CLI), navigate to this directory and run the following command to install requirements.
pip install -r requirements.txtIn your CLI, run the following command to initialize the app's database.
python manage.py migrate
Run the sample
Run the following command in your CLI to start the application.
python manage.py runserverOpen a browser and browse to
http://localhost:8000.
Code of conduct
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Disclaimer
THIS CODE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.