如何使用 python 访问 Azure AD 组列表? [英] How to access the list of Azure AD Groups using python?

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

问题描述

我是 Python 新手.我找到了以下示例代码来从 https://msdn.microsoft.com/en-us/Library/Azure/Ad/Graph/api/groups-operations#BasicoperationsongroupsGetgroups

I'm new to python. I found the following sample code to retrieve the Azure AD groups from https://msdn.microsoft.com/en-us/Library/Azure/Ad/Graph/api/groups-operations#BasicoperationsongroupsGetgroups

代码是:

########### Python 3.2 #############
import http.client, urllib.request, urllib.parse, urllib.error, base64

# OAuth2 is required to access this API. For more information visit: https://msdn.microsoft.com/en-us/office/office365/howto/common-app-authentication-tasks

headers = {}

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

try:
    conn = http.client.HTTPSConnection('graph.windows.net')
    #Specify values for path parameters (shown as {...}) and request body if needed
    conn.request("GET", "/myorganization/groups?%s" % params, "", headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

####################################

一切都很好,但我不知道headers = {}"的价值是什么.

everything is fine but i don't know what would be the value of "headers = {}".

我需要帮助,为此我花了很多时间,但还没有成功.

I need help i spent a lot of hours on this but no luck yet.

推荐答案

根据我的理解,需要将Oauth Authorization信息写入headers中,如下代码:

Base on my understanding, you need to write the Oauth Authorization information into headers, like the code below:

headers = {
    #set your access token
    'Authorization':'your access token'
}

在访问 Graph API 之前,您需要从 AD 中获取访问令牌.您可以将授权信息添加到您的标头和请求中.关于如何获取access token,建议大家可以参考这个页面:Active Directory -身份验证方案

Before you accessing the Graph API, you need to get the access token form AD. You can add the authorization information into your headers and request. About how to get the access token, I suggest that you can refer to this page: Active Directory -Authentication Scenarios

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

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