检索Azure Active Directory和本地帐户的数据

时间:2018-07-03 17:33:15

标签: python active-directory azure-active-directory microsoft-graph

我被告知,我正在创建一个必须显示活动目录数据的应用程序(该目录位于microsoft azure中),将通过HTTP GET使用Microsoft Graph API来完成对数据的请求要求。

除了以“天蓝色”方式“检查”活动目录数据外,我的应用还必须能够读取本地帐户的权限,以确保无法从活动目录中读取该内容,这意味着我将不得不检入计算机本身。对于这个问题,有人告诉我必须通过Powershell编程(以检索本地帐户的数据)来完成。

最后,我的应用程序必须在AWS Lambda上运行。

如果您能给我一种解决方案的方法,我将不仅仅提供代码。

我到目前为止所做的事情:

我用this code创建了一个python文件以连接到Microsoft图形API:

我在其中添加了一个令牌来访问图形API,但无法成功。 我从here获得了令牌,该令牌应该是Microsoft的示例令牌。现在,我不是在AWS Lambda上运行我的应用程序,而是在pycharm上运行它。

我在python上的代码可访问Azure Active Directory:

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 = {
    'Authorization': 'Bearer' 'EwAoA8l6BAAU7p9QDpi/D7xJLwsTgCg3TskyTaQAAXu71AU9f4aS4rOK5xoO/SU5HZKSXtCsDe0Pj7uSc5Ug008qTI+a9M1tBeKoTs7tHzhJNSKgk7pm5e8d3oGWXX5shyOG3cKSqgfwuNDnmmPDNDivwmi9kmKqWIC9OQRf8InpYXH7NdUYNwN+jljffvNTewdZz42VPrvqoMH7hSxiG7A1h8leOv4F3Ek/oeJX6U8nnL9nJ5pHLVuPWD0aNnTPTJD8Y4oQTp5zLhDIIfaJCaGcQperULVF7K6yX8MhHxIBwek418rKIp11om0SWBXOYSGOM0rNNN59qNiKwLNK+MPUf7ObcRBN5I5vg8jB7IMoz66jrNmT2uiWCyI8MmYDZgAACPoaZ9REyqke+AE1/x1ZX0w7OamUexKF8YGZiw+cDpT/BP1GsONnwI4a8M7HsBtDgZPRd6/Hfqlq3HE2xLuhYX8bAc1MUr0gP9KuH6HDQNlIV4KaRZWxyRo1wmKHOF5G5wTHrtxg8tnXylMc1PKOtaXIU4JJZ1l4x/7FwhPmg9M86PBPWr5zwUj2CVXC7wWlL/6M89Mlh8yXESMO3AIuAmEMKjqauPrgi9hAdI2oqnLZWCRL9gcHBida1y0DTXQhcwMv1ORrk65VFHtVgYAegrxu3NDoJiDyVaPZxDwTYRGjPII3va8GALAMVy5xou2ikzRvJjW7Gm3XoaqJCTCExN4m5i/Dqc81Gr4uT7OaeypYTUjnwCh7aMhsOTDJehefzjXhlkn//2eik+NivKx/BTJBEdT6MR97Wh/ns/VcK7QTmbjwbU2cwLngT7Ylq+uzhx54R9JMaSLhnw+/nIrcVkG77Hi3neShKeZmnl5DC9PuwIbtNvVge3Q+V0ws2zsL3z7ndz4tTMYFdvR/XbrnbEErTDLWrV6Lc3JHQMs0bYUyTBg5dThwCiuZ1evaT6BlMMLuSCVxdBGzXTBcvGwihFzZbyNoX+52DS5x+RbIEvd6KWOpQ6Ni+1GAawHDdNUiQTQFXRxLSHfc9fh7hE4qcD7PqHGsykYj7A0XqHCjbKKgWSkcAg=='
}

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

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/f795caea-121d-49c7-8ae6-a95623add8aa?%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))

这是执行代码后出现的错误:

C:\Users\Joan\PycharmProjects\Prueba\venv\Scripts\python.exe C:/Users/Joan/PycharmProjects/Prueba/GraphTest.py
b'{"odata.error":{"code":"Authentication_MissingOrMalformed","message":{"lang":"en","value":"Access Token missing or malformed."},"date":"2018-07-02T21:01:26","requestId":"c3429e83-9cee-4f79-bfa5-642aa8da482a","values":null}}'

Process finished with exit code 0

1 个答案:

答案 0 :(得分:2)

本节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GwihFzZbyNoX+52DS5x+RbIEvd6KWOpQ6Ni+1GAawHDdNUiQTQFXRxLSHfc9fh7hE4qcD7PqHGsykYj7A0XqHCjbKKgWSkcAg=='
}

我假设这只是一些临时POC代码,以防万一,请记住,您的令牌 将在1小时后过期,因此像这样进行硬编码是没有意义的。您将需要获取一个新令牌,并在进行调用时将其注入标头中。