Hi,
You can try to utilize the azure Identity library, which simplifies the process of obtaining tokens. This is the simple and straight forward for token acquisition.
Use the following Python code snippet to obtain an access token using the Managed Identity. Before that make sure 'azure-identity' is installed in your env.
from azure.identity import ManagedIdentityCredential
from azure.core.credentials import AccessToken
# Define the resource for which you need the token
resource = "https://<your-web-app-name>.azurewebsites.net"
# Create a Managed Identity Credential instance
credential = ManagedIdentityCredential()
# Acquire the token
token: AccessToken = credential.get_token(resource)
# Use the token for your HTTP requests
access_token = token.token
print(access_token)