Get all Active Directory / Entra ID users in your organization using a Python script

Would you like a list of all users in your organization? Including email, name, phone, etc? At the mere push of a button?

The Microsoft Graph API may help with this wish. Add a sprinkle of Python and you can automate this process leaving you to do other important and eh … fun things like … documentation?

I’ve written a script that helps you access all user data in your organization. You could use this script in a serverless computing solution like Azure Functions or AWS Lambda Function. Schedule the script and BAM:

You have (part of) your DimEmployee ready to be used!

In order to make your life easier I created a step-by-step tutorial. Check it out below!

Lader Bezig met laden…
EAD logo Duurt het te lang?

Opnieuw laden Laad het document opnieuw
| Open Openen in nieuwe tab
import requests
import json
import auth

# Get an access token from Microsoft Identity platform
tenant_id = auth.tenant_id
client_id = auth.client_id
client_secret = auth.client_secret
authority_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"

body = {
    "grant_type": "client_credentials",
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": "https://graph.microsoft.com/.default",
}

token_response = requests.post(authority_url, data=body)
token = token_response.json().get('access_token')

# Use the token to make an API request
headers = {
    "Authorization": f"Bearer {token}",
    "Content-Type": "application/json"
}

# API call to get all users
url = "https://graph.microsoft.com/v1.0/users"
response = requests.get(url, headers=headers)

# Print the result
users = response.json()
print(json.dumps(users, indent=4))

Leave Comment

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *