简体   繁体   中英

API to get Azure object-id from application-id

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?

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'

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM