英文:
retrieve the object_id of the Azure service principal
问题
如何使用Python API程序化地检索Azure服务主体的object_id?
英文:
How can I retrieve the object_id of an Azure service principal programatically using the Python APIs?
答案1
得分: 2
我尝试在我的环境中执行相同操作并获得以下结果:
检索Azure AD应用程序的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 = "/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服务主体(企业应用程序)的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()
英文:
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:
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()
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论