简体   繁体   English

如何使用 python 访问 Azure AD 组列表?

[英]How to access the list of Azure AD Groups using python?

I'm new to python.我是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我找到了以下示例代码来从https://msdn.microsoft.com/en-us/Library/Azure/Ad/Graph/api/groups-operations#BasicoperationsongroupsGetgroups检索 Azure AD 组

the code is:代码是:

########### 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))

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

everything is fine but i don't know what would be the value of "headers = {}".一切都很好,但我不知道“headers = {}”的价值是什么。

I need help i spent a lot of hours on this but no luck yet.我需要帮助,我在这上面花了很多时间,但还没有运气。

Base on my understanding, you need to write the Oauth Authorization information into headers, like the code below:根据我的理解,需要将 Oauth Authorization 信息写入 headers 中,如下代码:

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

Before you accessing the Graph API, you need to get the access token form AD.在访问 Graph API 之前,您需要从 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关于如何获取访问令牌,建议可以参考这个页面: Active Directory -Authentication Scenarios

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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