This sample is also available as a quickstart for the Microsoft identity platform: Quickstart: Add sign-in with Microsoft to a Python web app
This sample demonstrates a Python web application that signs-in users with the Microsoft identity platform and calls the Microsoft Graph.
This sample shows how to build a Python web app using Flask and MSAL Python, that signs in a user, and get access to Microsoft Graph. For more information about how the protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
To run this sample, you’ll need:
- Python 2.7+ or Python 3+
- An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see how to get an Azure AD tenant.
From your shell or command line:
git clone https://github.com/Azure-Samples/ms-identity-python-webapp.git
or download and extract the repository .zip file.
Given that the name of the sample is quite long, you might want to clone it in a folder close to the root of your hard drive, to avoid file name length limitations when running on Windows.
There is one project in this sample. To register it, you can:
If you want to use this automation:
In PowerShell run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
In PowerShell run:
cd .\AppCreationScripts\
.\Configure.ps1
cd ..
Other ways of running the scripts are described in App Creation Scripts
If you don’t want to use this automation, follow the steps below.
As a first step you’ll need to:
python-webapp
.http://localhost:5000/getAToken
.From the Certificates & secrets page, in the Client secrets section, choose New client secret:
app secret
),In the steps below, “ClientID” is the same as “Application ID” or “AppId”.
Note: if you used the setup scripts, the changes below may have been applied for you
app_config.py
fileEnter_the_Tenant_Name_Here
and replace the existing value with your Azure AD tenant name.python-webapp
app in the Azure portal.
Now you can set the secret in environment variable CLIENT_SECRET
,
and then adjust app_config.py
to pick it up.Enter_the_Application_Id_here
and replace the existing value with the application ID (clientId) of the python-webapp
application copied from the Azure portal.$ pip install -r requirements.txt
Run app.py from shell or command line. Note that the host and port values need to match what you’ve set up in your redirect_uri:
$ flask run --host localhost --port 5000
Use Stack Overflow to get support from the community.
Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.
Make sure that your questions or comments are tagged with [azure-active-directory
adal
msal
python
].
If you find a bug in the sample, please raise the issue on GitHub Issues.
To provide a recommendation, visit the following User Voice page.
If you’d like to contribute to this sample, see CONTRIBUTING.MD.
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.
For more information, see MSAL.Python’s conceptual documentation:
For more information about web apps scenarios on the Microsoft identity platform see Scenario: Web app that calls web APIs
For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.