I am having some challenges getting the application owner of each application registration. Anyone have any insight? Here is a snippet of the code i'm using.
import logging
import requests
import datetime
from datetime import timezone
from dateutil.parser import parse
def get_application_owner(graph_client, app_id):
"""
Retrieve owner of the Azure Application using Microsoft Graph Client.
"""
# Get the application registration details
application = graph_client.get(f"/applications/{app_id}")
application_json = application.json()
owner_object_id = application_json.get('appOwnerOrganizationId')
if owner_object_id:
# Retrieve the owner's details
owner = graph_client.get(f"/users/{owner_object_id}")
owner_json = owner.json()
return owner_json.get('displayName', 'N/A')
else:
return 'N/A'
credential = ClientSecretCredential(tenant_id,client_id,client_secret)
graph_base_url = "https://graph.microsoft.com/v1.0"
graph_client = GraphClient(credential=credential)
graph_url="/v1.0/serviceprincipals"
response = graph_client.get(graph_url)
service_principals = response.json().get('value', [])
for app in service_principals:
app_id = app.get('appId')
owner_object_id = app.get('appOwnerOrganizationId')
owner_name = 'N/A'
if owner_object_id:
owner = graph_client.objects.get_object_by_object_id(owner_object_id)
owner_name = owner.display_name
secrets = app.get('passwordCredentials', [])
for secret in secrets:
end_date_str = secret.get('endDateTime', None)
if end_date_str:
end_date = parse(end_date_str)
if end_date <= expiry_threshold:
human_friendly_date = end_date.strftime('%Y-%m-%d %H:%M:%S %Z')
expiring_secrets.append({
'displayName': app.get('displayName'),
'secretId': secret.get('keyId'),
'expiryDate': human_friendly_date,
'owner': owner_name
})
I've used these libraries above but I do not get the owner as expected: