gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:38:52 28 4
gpt4 key购买 nike

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

代码是:

########### 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 = {}”的值是什么。

我需要帮助,我花了很多时间在这上面,但还没有运气。

最佳答案

根据我的理解,您需要将Oauth授权信息写入 header ,如下代码:

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

在访问 Graph API 之前,您需要从 AD 获取访问 token 。您可以将授权信息添加到 header 和请求中。关于如何获取access token,建议您可以引用这个页面: Active Directory -Authentication Scenarios

关于python - 如何使用 python 访问 Azure AD 组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30849305/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com