Home > Software design >  API to get Azure object-id from application-id
API to get Azure object-id from application-id

Time:12-03

I have an application which is registered in Azure (followed this guide). I have the application-id for this application, and I would like to find its object-id by using Azure API (I am using python library). How can I retrieve the object-id?

CodePudding user response:

If you need the object id for the service principal, you can use the service principal list endpoint with a filter. https://learn.microsoft.com/en-us/graph/api/serviceprincipal-list?view=graph-rest-1.0&tabs=http

Example filter: $filter=appId eq 'your-app-id'

CodePudding user response:

I found that msgraph-core (in preview) can be used for this purpose.

Sample code:

def get_object_id(self, app_id, tenant_id, client_id, client_secret):
    credential = ClientSecretCredential(tenant_id, client_id, client_secret)        
    client = GraphClient(credential=credential)
    endpoint = "/servicePrincipals"
    select = "displayName,id,appId"
    req_filter = f"appId eq '{app_id}'"
    request_url = f'{endpoint}?$select={select}&$filter={req_filter}'

    response = client.get(request_url)
    return response.json()['value'][0]['id']
  • Related