Created
May 18, 2023 23:20
-
-
Save strikaco/2e00d8d4ed19c09811c84903b0f8505a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
# Define your Azure app registration details | |
client_id = 'your_client_id' | |
client_secret = 'your_client_secret' | |
tenant_id = 'your_tenant_id' | |
authority = f"https://login.microsoftonline.com/{tenant_id}" | |
scope = ["https://graph.microsoft.com/.default"] | |
graph_url = 'https://graph.microsoft.com/v1.0/' | |
def get_token(): | |
data = { | |
'client_id': client_id, | |
'scope': scope, | |
'client_secret': client_secret, | |
'grant_type': 'client_credentials' | |
} | |
response = requests.post(authority + '/oauth2/v2.0/token', data=data) | |
response.raise_for_status() | |
return response.json()['access_token'] | |
def list_groups(token): | |
headers = { | |
'Authorization': 'Bearer ' + token | |
} | |
response = requests.get(graph_url + 'groups', headers=headers) | |
response.raise_for_status() | |
return response.json() | |
def main(): | |
token = get_token() | |
groups_data = list_groups(token) | |
for group in groups_data['value']: | |
print(group['displayName']) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment