访问从Microsoft Graph API获得的特定JSON嵌套数据

时间:2018-08-01 20:37:39

标签: python json microsoft-graph

当前,我正在开发一个应用程序,在该应用程序中,我通过Microsoft的graph API从azure活动目录中检索了数据,我将不得不使用此数据将其显示在html表中,因为获取数据的方式并不容易要读取,json格式文件存储在graph_data变量中,您将在以后看到:

value [  
       {  
          'id':'38d0d6ab-3c35-407a-9524-b9bdc84791e3',
          'deletedDateTime':None,
          'classification':None,
          'createdDateTime':'2018-07-05T14:46:35Z',
          'creationOptions':[  

          ],
      'description':'Members of this group receive staff filtering policies',
      'displayName':'staff',
      'groupTypes':[  

      ],
      'mail':None,
      'mailEnabled':False,
      'mailNickname':'fdbdda4e-1b03-4668-98b0-5cafbc3f218b',
      'onPremisesLastSyncDateTime':None,
      'onPremisesProvisioningErrors':[  

      ],
      'onPremisesSecurityIdentifier':None,
      'onPremisesSyncEnabled':None,
      'preferredDataLocation':None,
      'proxyAddresses':[  

      ],
      'renewedDateTime':'2018-07-05T14:46:35Z',
      'resourceBehaviorOptions':[  

      ],
      'resourceProvisioningOptions':[  

      ],
      'securityEnabled':True,
      'visibility':None,
      'members':[  
         {  
            '@odata.type':'#microsoft.graph.user',
            'id':'a205ea5f-f498-4841-b487-72398c88a3d6',
            'deletedDateTime':None,
            'accountEnabled':True,
            'ageGroup':'Undefined',
            'businessPhones':[  

            ],
            'city':None,
            'companyName':None,
            'consentProvidedForMinor':'None',
            'country':None,
            'createdDateTime':None,
            'department':'Information Technology',
            'displayName':'Yipin Lu',
            'givenName':'Yipin',
            'jobTitle':'Consultant',
            'legalAgeGroupClassification':'Undefined',
            'mail':None,
            'mailNickname':'yipinlu',
            'mobilePhone':None,
            'onPremisesImmutableId':None,
            'onPremisesLastSyncDateTime':None,
            'onPremisesProvisioningErrors':[  

            ],
            'onPremisesSecurityIdentifier':None,
            'onPremisesSyncEnabled':None,
            'passwordPolicies':None,
            'passwordProfile':None,
            'officeLocation':None,
            'onPremisesExtensionAttributes':{  
               'extensionAttribute1':None,
               'extensionAttribute2':None,
               'extensionAttribute3':None,
               'extensionAttribute4':None,
               'extensionAttribute5':None,
               'extensionAttribute6':None,
               'extensionAttribute7':None,
               'extensionAttribute8':None,
               'extensionAttribute9':None,
               'extensionAttribute10':None,
               'extensionAttribute11':None,
               'extensionAttribute12':None,
               'extensionAttribute13':None,
               'extensionAttribute14':None,
               'extensionAttribute15':None
            },
            'postalCode':None,
            'preferredLanguage':None,
            'proxyAddresses':[  

            ],
            'imAddresses':[  

            ],
            'isResourceAccount':None,
            'state':None,
            'streetAddress':None,
            'surname':'Lu',
            'usageLocation':None,
            'userPrincipalName':'yipinlu@joanperez5hotmail.onmicrosoft.com',
            'userType':'Member',
            'assignedLicenses':[  

            ],
            'assignedPlans':[  

            ],
            'provisionedPlans':[  

            ]
         },
         {  
            '@odata.type':'#microsoft.graph.user',
            'id':'fa576dbe-3f7f-44f6-9db1-60908999f3f9',
            'deletedDateTime':None,
            'accountEnabled':True,
            'ageGroup':'Undefined',
            'businessPhones':[  

            ],
            'city':None,
            'companyName':None,
            'consentProvidedForMinor':'None',
            'country':None,
            'createdDateTime':None,
            'department':'Information Technology',
            'displayName':'Joan Perez',
            'givenName':'Joan',
            'jobTitle':'Consultant',
            'legalAgeGroupClassification':'Undefined',
            'mail':None,
            'mailNickname':'joanperez',
            'mobilePhone':None,
            'onPremisesImmutableId':None,
            'onPremisesLastSyncDateTime':None,
            'onPremisesProvisioningErrors':[  

            ],
            'onPremisesSecurityIdentifier':None,
            'onPremisesSyncEnabled':None,
            'passwordPolicies':None,
            'officeLocation':None,
            'onPremisesExtensionAttributes':{  
               'extensionAttribute1':None,
               'extensionAttribute2':None,
               'extensionAttribute3':None,
               'extensionAttribute4':None,
               'extensionAttribute5':None,
               'extensionAttribute6':None,
               'extensionAttribute7':None,
               'extensionAttribute8':None,
               'extensionAttribute9':None,
               'extensionAttribute10':None,
               'extensionAttribute11':None,
               'extensionAttribute12':None,
               'extensionAttribute13':None,
               'extensionAttribute14':None,
               'extensionAttribute15':None
            },
            'postalCode':None,
            'preferredLanguage':None,
            'proxyAddresses':[  

            ],
            'imAddresses':[  

            ],
            'isResourceAccount':None,
            'state':None,
            'streetAddress':None,
            'surname':'Perez Lozano',
            'usageLocation':None,
            'userPrincipalName':'joanperez@joanperez5hotmail.onmicrosoft.com',
            'userType':'Member',
            'assignedLicenses':[  

            ],
            'assignedPlans':[  

            ],
            'passwordProfile':{  
               'password':None,
               'forceChangePasswordNextSignIn':True
            },
            'provisionedPlans':[  

            ]
         },
         {  
            '@odata.type':'#microsoft.graph.user',
            'id':'d3902696-5de8-40ad-ad25-6bc72a58306b',
            'deletedDateTime':None,
            'accountEnabled':True,
            'ageGroup':'Undefined',
            'businessPhones':[  

            ],
            'city':None,
            'companyName':None,
            'consentProvidedForMinor':'None',
            'country':None,
            'createdDateTime':None,
            'department':'Information Technology',
            'displayName':'Dayamarali Espinosa',
            'givenName':'Dayamarli',
            'jobTitle':'Consultant',
            'legalAgeGroupClassification':'Undefined',
            'mail':None,
            'mailNickname':'dayaespinoza',
            'mobilePhone':None,
            'onPremisesImmutableId':None,
            'onPremisesLastSyncDateTime':None,
            'onPremisesProvisioningErrors':[  

            ],
            'onPremisesSecurityIdentifier':None,
            'onPremisesSyncEnabled':None,
            'passwordPolicies':None,
            'officeLocation':None,
            'onPremisesExtensionAttributes':{  
               'extensionAttribute1':None,
               'extensionAttribute2':None,
               'extensionAttribute3':None,
               'extensionAttribute4':None,
               'extensionAttribute5':None,
               'extensionAttribute6':None,
               'extensionAttribute7':None,
               'extensionAttribute8':None,
               'extensionAttribute9':None,
               'extensionAttribute10':None,
               'extensionAttribute11':None,
               'extensionAttribute12':None,
               'extensionAttribute13':None,
               'extensionAttribute14':None,
               'extensionAttribute15':None
            },
            'postalCode':None,
            'preferredLanguage':None,
            'proxyAddresses':[  

            ],
            'imAddresses':[  

            ],
            'isResourceAccount':None,
            'state':None,
            'streetAddress':None,
            'surname':'Espinosa',
            'usageLocation':None,
            'userPrincipalName':'dayaespinosa@joanperez5hotmail.onmicrosoft.com',
            'userType':'Member',
            'assignedLicenses':[  

            ],
            'assignedPlans':[  

            ],
            'passwordProfile':{  
               'password':None,
               'forceChangePasswordNextSignIn':True
            },
            'provisionedPlans':[  

            ]
         }
      ]
   },
   {  
      'id':'3a2141f3-b1ed-4d79-a88f-3bcff708957e',
      'deletedDateTime':None,
      'classification':None,
      'createdDateTime':'2018-07-10T20:54:55Z',
      'creationOptions':[  

      ],
      'description':'This group of users are the summer interns of 2018',
      'displayName':'Interns',
      'groupTypes':[  

      ],
      'mail':None,
      'mailEnabled':False,
      'mailNickname':'a3264998-68b2-4174-81d3-74b1b2238091',
      'onPremisesLastSyncDateTime':None,
      'onPremisesProvisioningErrors':[  

      ],
      'onPremisesSecurityIdentifier':None,
      'onPremisesSyncEnabled':None,
      'preferredDataLocation':None,
      'proxyAddresses':[  

      ],
      'renewedDateTime':'2018-07-10T20:54:55Z',
      'resourceBehaviorOptions':[  

      ],
      'resourceProvisioningOptions':[  

      ],
      'securityEnabled':True,
      'visibility':None,
      'members':[  
         {  
            '@odata.type':'#microsoft.graph.user',
            'id':'0efcf3fe-86a0-48dc-a698-13eed7e0095b',
            'deletedDateTime':None,
            'accountEnabled':True,
            'ageGroup':None,
            'businessPhones':[  

            ],
            'city':None,
            'companyName':None,
            'consentProvidedForMinor':None,
            'country':None,
            'createdDateTime':None,
            'department':'Information Technology',
            'displayName':'Cristiano Ronaldo',
            'givenName':'Cristiano',
            'jobTitle':'Intern',
            'legalAgeGroupClassification':None,
            'mail':None,
            'mailNickname':'cristianoro',
            'mobilePhone':None,
            'onPremisesImmutableId':None,
            'onPremisesLastSyncDateTime':None,
            'onPremisesProvisioningErrors':[  

            ],
            'onPremisesSecurityIdentifier':None,
            'onPremisesSyncEnabled':None,
            'passwordPolicies':None,
            'officeLocation':None,
            'onPremisesExtensionAttributes':{  
               'extensionAttribute1':None,
               'extensionAttribute2':None,
               'extensionAttribute3':None,
               'extensionAttribute4':None,
               'extensionAttribute5':None,
               'extensionAttribute6':None,
               'extensionAttribute7':None,
               'extensionAttribute8':None,
               'extensionAttribute9':None,
               'extensionAttribute10':None,
               'extensionAttribute11':None,
               'extensionAttribute12':None,
               'extensionAttribute13':None,
               'extensionAttribute14':None,
               'extensionAttribute15':None
            },
            'postalCode':None,
            'preferredLanguage':None,
            'proxyAddresses':[  

            ],
            'imAddresses':[  

            ],
            'isResourceAccount':None,
            'state':None,
            'streetAddress':None,
            'surname':'Ronaldo',
            'usageLocation':None,
            'userPrincipalName':'cristianoro@joanperez5hotmail.onmicrosoft.com',
            'userType':'Member',
            'assignedLicenses':[  

            ],
            'assignedPlans':[  

            ],
            'passwordProfile':{  
               'password':None,
               'forceChangePasswordNextSignIn':True
            },
            'provisionedPlans':[  

            ]
         },
         {  
            '@odata.type':'#microsoft.graph.user',
            'id':'3009b0bb-8420-4a76-8313-300f3d6559c0',
            'deletedDateTime':None,
            'accountEnabled':True,
            'ageGroup':None,
            'businessPhones':[  

            ],
            'city':None,
            'companyName':None,
            'consentProvidedForMinor':None,
            'country':None,
            'createdDateTime':None,
            'department':'Information Technology',
            'displayName':'Lionel Messi',
            'givenName':'Lionel',
            'jobTitle':'Intern',
            'legalAgeGroupClassification':None,
            'mail':None,
            'mailNickname':'lionelme',
            'mobilePhone':None,
            'onPremisesImmutableId':None,
            'onPremisesLastSyncDateTime':None,
            'onPremisesProvisioningErrors':[  

            ],
            'onPremisesSecurityIdentifier':None,
            'onPremisesSyncEnabled':None,
            'passwordPolicies':None,
            'officeLocation':None,
            'onPremisesExtensionAttributes':{  
               'extensionAttribute1':None,
               'extensionAttribute2':None,
               'extensionAttribute3':None,
               'extensionAttribute4':None,
               'extensionAttribute5':None,
               'extensionAttribute6':None,
               'extensionAttribute7':None,
               'extensionAttribute8':None,
               'extensionAttribute9':None,
               'extensionAttribute10':None,
               'extensionAttribute11':None,
               'extensionAttribute12':None,
               'extensionAttribute13':None,
               'extensionAttribute14':None,
               'extensionAttribute15':None
            },
            'postalCode':None,
            'preferredLanguage':None,
            'proxyAddresses':[  

            ],
            'imAddresses':[  

            ],
            'isResourceAccount':None,
            'state':None,
            'streetAddress':None,
            'surname':'Messi',
            'usageLocation':None,
            'userPrincipalName':'lionelme@joanperez5hotmail.onmicrosoft.com',
            'userType':'Member',
            'assignedLicenses':[  

            ],
            'assignedPlans':[  

            ],
            'passwordProfile':{  
               'password':None,
               'forceChangePasswordNextSignIn':True
            },
            'provisionedPlans':[  

            ]
         },
         {  
            '@odata.type':'#microsoft.graph.user',
            'id':'4fc82cba-ee4a-4829-a722-b5df1ee02188',
            'deletedDateTime':None,
            'accountEnabled':True,
            'ageGroup':None,
            'businessPhones':[  

            ],
            'city':None,
            'companyName':None,
            'consentProvidedForMinor':None,
            'country':None,
            'createdDateTime':None,
            'department':'Information Technology',
            'displayName':'Antoine Griezmann',
            'givenName':'Antoine',
            'jobTitle':'Intern',
            'legalAgeGroupClassification':None,
            'mail':None,
            'mailNickname':'antoinegr',
            'mobilePhone':None,
            'onPremisesImmutableId':None,
            'onPremisesLastSyncDateTime':None,
            'onPremisesProvisioningErrors':[  

            ],
            'onPremisesSecurityIdentifier':None,
            'onPremisesSyncEnabled':None,
            'passwordPolicies':None,
            'officeLocation':None,
            'onPremisesExtensionAttributes':{  
               'extensionAttribute1':None,
               'extensionAttribute2':None,
               'extensionAttribute3':None,
               'extensionAttribute4':None,
               'extensionAttribute5':None,
               'extensionAttribute6':None,
               'extensionAttribute7':None,
               'extensionAttribute8':None,
               'extensionAttribute9':None,
               'extensionAttribute10':None,
               'extensionAttribute11':None,
               'extensionAttribute12':None,
               'extensionAttribute13':None,
               'extensionAttribute14':None,
               'extensionAttribute15':None
            },
            'postalCode':None,
            'preferredLanguage':None,
            'proxyAddresses':[  

            ],
            'imAddresses':[  

            ],
            'isResourceAccount':None,
            'state':None,
            'streetAddress':None,
            'surname':'Griezmann',
            'usageLocation':None,
            'userPrincipalName':'antoinegr@joanperez5hotmail.onmicrosoft.com',
            'userType':'Member',
            'assignedLicenses':[  

            ],
            'assignedPlans':[  

            ],
            'passwordProfile':{  
               'password':None,
               'forceChangePasswordNextSignIn':True
            },
            'provisionedPlans':[  

            ]
         },
         {  
            '@odata.type':'#microsoft.graph.user',
            'id':'58f4b229-293b-4890-be5a-724dfcb59565',
            'deletedDateTime':None,
            'accountEnabled':True,
            'ageGroup':None,
            'businessPhones':[  

            ],
            'city':None,
            'companyName':None,
            'consentProvidedForMinor':None,
            'country':None,
            'createdDateTime':None,
            'department':'Information Technology',
            'displayName':'Paul Pogba',
            'givenName':'Paul',
            'jobTitle':'Intern',
            'legalAgeGroupClassification':None,
            'mail':None,
            'mailNickname':'paulpo',
            'mobilePhone':None,
            'onPremisesImmutableId':None,
            'onPremisesLastSyncDateTime':None,
            'onPremisesProvisioningErrors':[  

            ],
            'onPremisesSecurityIdentifier':None,
            'onPremisesSyncEnabled':None,
            'passwordPolicies':None,
            'officeLocation':None,
            'onPremisesExtensionAttributes':{  
               'extensionAttribute1':None,
               'extensionAttribute2':None,
               'extensionAttribute3':None,
               'extensionAttribute4':None,
               'extensionAttribute5':None,
               'extensionAttribute6':None,
               'extensionAttribute7':None,
               'extensionAttribute8':None,
               'extensionAttribute9':None,
               'extensionAttribute10':None,
               'extensionAttribute11':None,
               'extensionAttribute12':None,
               'extensionAttribute13':None,
               'extensionAttribute14':None,
               'extensionAttribute15':None
            },
            'postalCode':None,
            'preferredLanguage':None,
            'proxyAddresses':[  

            ],
            'imAddresses':[  

            ],
            'isResourceAccount':None,
            'state':None,
            'streetAddress':None,
            'surname':'Pogba',
            'usageLocation':None,
            'userPrincipalName':'paulpo@joanperez5hotmail.onmicrosoft.com',
            'userType':'Member',
            'assignedLicenses':[  

            ],
            'assignedPlans':[  

            ],
            'passwordProfile':{  
               'password':None,
               'forceChangePasswordNextSignIn':True
            },
            'provisionedPlans':[  

            ]
         }
      ]
   }
]

此数据是蔚蓝活动目录中的组及其用户的列表。 因此,在html表中,我想知道如何列出组的显示名称和用户的主体名称。

这是我应用HTTP请求的代码:

import adal
import flask #web framework
import uuid
import requests

import config

app = flask.Flask(__name__)
app.debug = True
app.secret_key = 'development'

PORT = 5000  # A flask app by default runs on PORT 5000
AUTHORITY_URL = config.AUTHORITY_HOST_URL + '/' + config.TENANT
REDIRECT_URI = 'http://localhost:{}/getAToken'.format(PORT)
TEMPLATE_AUTHZ_URL = ('https://login.microsoftonline.com/{}/oauth2/authorize?' +
                      'response_type=code&client_id={}&redirect_uri={}&' +
                      'state={}&resource={}')


@app.route("/")
def main():
    login_url = 'http://localhost:{}/login'.format(PORT)
    resp = flask.Response(status=307)
    resp.headers['location'] = login_url
    return resp


@app.route("/login")
def login():
    auth_state = str(uuid.uuid4())
    flask.session['state'] = auth_state
    authorization_url = TEMPLATE_AUTHZ_URL.format(
        config.TENANT,
        config.CLIENT_ID,
        REDIRECT_URI,
        auth_state,
        config.RESOURCE)
    resp = flask.Response(status=307)
    resp.headers['location'] = authorization_url
    return resp


@app.route("/getAToken")
def main_logic():
    code = flask.request.args['code']
    state = flask.request.args['state']
    if state != flask.session['state']:
        raise ValueError("State does not match")
    auth_context = adal.AuthenticationContext(AUTHORITY_URL)
    token_response = auth_context.acquire_token_with_authorization_code(code, REDIRECT_URI, config.RESOURCE,
                                                                        config.CLIENT_ID, config.CLIENT_SECRET)
    # It is recommended to save this to a database when using a production app.
    flask.session['access_token'] = token_response['accessToken']

    return flask.redirect('/graphcall')


@app.route('/graphcall')
def graphcall():
    if 'access_token' not in flask.session:
        return flask.redirect(flask.url_for('login'))
    endpoint = config.RESOURCE + '/' + config.API_VERSION + '/groups/?$expand=members' #https://graph.microsoft.com/v1.0/groups/
    http_headers = {'Authorization': flask.session.get('access_token'),
                    'User-Agent': 'adal-python-sample',
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    'client-request-id': str(uuid.uuid4())}
    graph_data = requests.get(endpoint, headers=http_headers, stream=False).json()

    return flask.render_template('homePage_2.html', graph_data=graph_data)


if __name__ == "__main__":
    app.run()

可以从我的html文件访问此graph_data变量,这是我尝试获取我需要的数据:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ADVS</title>
</head>
<body>
    <h1 style="text-align:center">Active Directory Viewer System</h1>
    <h2>Welcome, Yipin!</h2>
<p1><b>User Data:</b> </p1>


<table>
    <th>Display Name Group</th>
    <th>Description of Group</th>
    <th>Users</th>
    {% for gd in graph_data.value %}
        <tr>

            <td>{{ gd.displayname }}</td><!-- TRYING TO ACCESS THE GROUP DISPLAYNAME-->
<!-- <td>{{ gd.description }}</td> TRYING RO ACCESS THE group description-->
            <!-- <td>{{ gd.user.displayName }}</td> TRYING TO ACCESS THE USERS DISPLAYNAME-->

        </tr>
    {% endfor %}
</table>


</body>
</html>

当我执行我的应用程序时,请求我想要的数据时,我得到了没有数据的html表:

enter image description here

谢谢!

0 个答案:

没有答案