获取Azure服务主体的object_id。

huangapple go评论67阅读模式
英文:

retrieve the object_id of the Azure service principal

问题

如何使用Python API程序化地检索Azure服务主体的object_id?

获取Azure服务主体的object_id。

英文:

How can I retrieve the object_id of an Azure service principal programatically using the Python APIs?

获取Azure服务主体的object_id。

答案1

得分: 2

我尝试在我的环境中执行相同操作并获得以下结果:

检索Azure AD应用程序的ObjectID,请尝试以下代码:

获取Azure服务主体的object_id。

from azure.identity import ClientSecretCredential
from azure.graphrbac import GraphRbacManagementClient
from msgraph.core import GraphClient

def get_object_id():
    app_id = "AppID"
    tenant_id = "TenantID"
    client_id = "ClientID"
    client_secret = "ClientSecret"
    credential = ClientSecretCredential(tenant_id, client_id, client_secret)        
    client = GraphClient(credential=credential)
    endpoint = "/applications"
    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)
    print(response.json()['value'][0]['id'])
get_object_id()

获取Azure服务主体的object_id。

要获取Azure服务主体(企业应用程序)的ObjectID,请尝试以下代码:

from azure.identity import ClientSecretCredential
from azure.graphrbac import GraphRbacManagementClient
from msgraph.core import GraphClient

def get_object_id():
    app_id = "AppID"
    tenant_id = "TenantID"
    client_id = "ClientID"
    client_secret = "ClientSecret"
    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)
    print(response.json()['value'][0]['id'])
get_object_id()

获取Azure服务主体的object_id。

英文:

I attempted to do the same in my environment and got the following results:

To retrieve the ObjectID of the Azure AD Application try the below code:

获取Azure服务主体的object_id。

from azure.identity import ClientSecretCredential
from azure.graphrbac import GraphRbacManagementClient
from msgraph.core import GraphClient

def get_object_id():
    app_id = "AppID"
    tenant_id = "TenantID"
    client_id = "ClientID"
    client_secret = "ClientSecret"
    credential = ClientSecretCredential(tenant_id, client_id, client_secret)        
    client = GraphClient(credential=credential)
    endpoint = "/applications"
    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)
    print(response.json()['value'][0]['id'])
get_object_id()

获取Azure服务主体的object_id。

To get the ObjectID of the Azure Service Principal (Enterprise Application) try the below code:

from azure.identity import ClientSecretCredential
from azure.graphrbac import GraphRbacManagementClient
from msgraph.core import GraphClient

def get_object_id():
    app_id = "AppID"
    tenant_id = "TenantID"
    client_id = "ClientID"
    client_secret = "ClientSecret"
    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)
    print(response.json()['value'][0]['id'])
get_object_id()

获取Azure服务主体的object_id。

huangapple
  • 本文由 发表于 2023年4月17日 20:35:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定