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

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

问题描述

我是python的新手。我发现以下示例代码可从 https://msdn.microsoft.com/zh-CN/Library/Azure/Ad/Graph/api/groups-operations#BasicoperationsongroupsGetgroups



代码是:

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

#必须使用OAuth2才能访问此API。有关更多信息,请访问:https://msdn.microsoft.com/zh-cn/office/office365/howto/common-app-authentication-tasks

标头= {}

params = urllib.parse.urlencode({
#指定以下必需参数的值
'api-version':'1.5',
})

尝试:
conn = http.client.HTTPSConnection('graph.windows.net')
#指定路径参数的值(显示为{...}),并在需要时请求正文
conn.request( GET, / myorganization / groups?%s%params,,标头)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
例外,例如e:
print( [Errno {0}] {1}。format(e.errno,e。 strerror))

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

一切都很好,但是我不知道 headers = {}的值是什么。 p>

我需要帮助,为此花了很多时间,但还没有运气。

解决方案

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

  headers = {
#设置您的访问令牌
'授权':'您的访问令牌'
}

在访问Graph API之前,您需要获取访问令牌形式AD。您可以将授权信息添加到标题和请求中。
关于如何获取访问令牌,建议您参考以下页面:
活动目录-身份验证方案


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

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 = {}".

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:

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

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天全站免登陆