如何使用 python 访问 Azure AD 组和用户详细信息? [英] How to access the Azure AD Groups and user details using python?

查看:38
本文介绍了如何使用 python 访问 Azure AD 组和用户详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

params = urllib.urlencode({
    # Specify values for the following required parameters
    'api-version': '1.5',
    'tenant_id':'vvvvvvvvXXXXXX',
})

headers = { 'Authorization':'TzmMKl1QoxWjvPyX8Xv79ZxvZgoGHwbRt3ZQXwNoFBu42R6yj0o4aMraEVkNkoLyvN8KZjDi4mD7w41gTREsUhbOyg_PsUEv7g4SoTsbRluj8hHrrWuXj8h32MyklOB7ahAKBRLE8KAcmVARdb4vpQ'

}
try:
        conn = httplib.HTTPSConnection('graph.windows.net')
        print("got connection and getting it to actual domain")
        print(conn)
        conn.request("GET", "/{tenent_id}/groups?%s" % params, "", headers)
        response = conn.getresponse()
        data = response.read()
        print(data)
        conn.close()

但我收到以下错误:

连接尝试失败,因为连接方在一段时间后没有正确响应,或者因为连接的主机没有响应而建立连接失败

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

推荐答案

你可以试试下面的方法

from azure.common.credentials import ServicePrincipalCredentials
from azure.graphrbac import GraphRbacManagementClient

credentials = ServicePrincipalCredentials(
    client_id="Your_Client_ID",
    secret="Your_Secret",
    resource="https://graph.windows.net",
    tenant = 'yourtenant.onmicrosoft.com'
)
tenant_id = 'your_tenant_id'

graphrbac_client = GraphRbacManagementClient(
    credentials,
    tenant_id
)
users = graphrbac_client.users.list()
for user in users:
     print(user.user_principal_name)

groups = graphrbac_client.groups.list()
for g in groups:
     print(g.display_name)

<小时>

OR 使用 ADAL 和请求


OR Using ADAL and Requests

import adal,requests

url = 'https://login.microsoftonline.com/yourtenant.onmicrosoft.com/oauth2/v2.0/token'
data = {
    'grant_type': 'client_credentials',
    'client_id': "your_client_id",
    'scope': 'https://graph.microsoft.com/.default',
    'client_secret': "your_client_secret"
}
r = requests.post(url, data=data)
token = r.json().get('access_token')

url = 'https://graph.microsoft.com/v1.0/users'
#url = 'https://graph.microsoft.com/beta/groups'
headers = {
    'Content-Type' : 'application\json',
    'Authorization': 'Bearer {}'.format(token)
}
r = requests.get(url, headers=headers)
result = r.json()
print(result)

这篇关于如何使用 python 访问 Azure AD 组和用户详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆