Photo by Ed Hardie on Unsplash

Azure Active Directory And Python: How to Authenticate Using OAuth2 with Examples

Anthony

--

Authenticating into Azure AD allows your Python applications to access and manage various Azure resources, such as virtual machines, databases, storage, and more.

In testing, one way we can use Azure AD in our scripts is for API testing against Microsoft Dynamics.

In this article, we will provide the code and demonstrate how to connect to Azure AD using Python. We will also test our connection for a 200 response. Or, jump straight to the code.

Prerequisites

  • You have the Azure Tenant ID
  • You have the Client ID and Client Secret from the App Registration
  • The required packages have been installed. You can install the required packages by running:
pip install requests requests-oauthlib

1. Add the required imports

These requests libraries allow us to fetch the access token from the provider.

import requests
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session

2. Provide the OAuth2 credentials

--

--

Anthony

Join me as I share general, technical, and practical knowledge in the QA field.