I am trying working with microsoft Graph API, where my goal is to find out all the owners of a group in O365. For this I am using two APIs. 1)https://graph.microsoft.com/v1.0/groups/ -> to find out all the GroupIDs 2)https://graph.microsoft.com/v1.0/groups/{GroupID}/owners/ -> to find all the owners of that group.
I am usiing the below code to call both the APIs and finally inserting the data into Oracle DB. But I am getting KeyError.
url = "https://graph.microsoft.com/v1.0/groups/"
Group_owner_url = "https://graph.microsoft.com/v1.0/groups/{}/owners/"
rows =[]
sysdate = datetime.datetime.now()
response = requests.request("GET", url, headers=headers, data=payload)
data = response.json()
for group in data['value']:
GroupID = group['id']
rows.append(GroupID)
for GroupID in rows:
url_owner = Group_owner_url.format(GroupID)
response_owner = requests.request("GET", url_owner, headers=headers, data=payload)
owner_data = response_owner.json()
for owner in owner_data['value']:
cursor.execute(
"insert into <tabel> (GROUP_ID, OWNER_NAME, OWNER_GIVENNAME, OWNER_SURNAME, JOB_TITLE, USER_PRINCIPAL_NAME, EMAIL, MOBILE_PHONE, OFFICE_LOCATION, DB_CREATE_TIME)values (:1, :2, :3, :4, :5, :6, :7, :8, :9, :10)",(GroupID, owner['displayName'],owner['givenName'], owner['surname'], owner['jobTitle'], owner['userPrincipalName'], owner['mail'], owner['mobilePhone'], owner['officeLocation'], sysdate))
cursor.close()
connect.commit()
connect.close()
But i am getting this error: "for owner in owner_data['value']: KeyError: 'value' "
Where I am making the mistake? for the ref. the sample API response is as below:
{
'@odata.context': 'https://graph.microsoft.com/v1.0/$metadata#directoryObjects',
'value': [{
'@odata.type': '#microsoft.graph.user',
'id': '56tyu66j7-e802-4414-0e4878',
'businessPhones': ['(888) v45-89nn'],
'displayName': 'Wood, Michel',
'givenName': 'Michel',
'jobTitle': 'Dir School Of Commerce',
'mail': 'iammich@xyz.com',
'mobilePhone': None,
'officeLocation': '456 Marshal Building',
'preferredLanguage': None,
'surname': 'Wood',
'userPrincipalName': 'iammich@xyz.com'
}
]
}{
'@odata.context': 'https://graph.microsoft.com/v1.0/$metadata#directoryObjects',
'value': []
}{
'@odata.context': 'https://graph.microsoft.com/v1.0/$metadata#directoryObjects',
'value': [{
'@odata.type': '#microsoft.graph.user',
'id': '1248hju-73b0-4216-fd189',
'businessPhones': ['(888) 888-8888'],
'displayName': 'Verana, Eagle',
'givenName': 'Eagle',
'jobTitle': 'Manager',
'mail': 'vrmegl@xys.com',
'mobilePhone': '(***) ***-****',
'officeLocation': '40 Cmmunication',
'preferredLanguage': None,
'surname': 'Verana',
'userPrincipalName': 'vrmegl@xys.com'
}
]
}
CodePudding user response:
After exploring and trying so many paths finally got it. Here is the correct solution:
for GroupID in rows:
url_owner = Group_owner_url.format(GroupID)
response_owner = requests.request("GET", url_owner, headers=headers, data=payload)
owner_data = response_owner.json()
all_owner = owner_data.get('value')
# added this for loop and .get() in the SQL statement
for owner in all_owner or []:
cursor.execute(
"insert into <tabel> (GROUP_ID, OWNER_NAME, OWNER_GIVENNAME, OWNER_SURNAME, JOB_TITLE, USER_PRINCIPAL_NAME, EMAIL, MOBILE_PHONE, OFFICE_LOCATION, DB_CREATE_TIME)values (:1, :2, :3, :4, :5, :6, :7, :8, :9, :10)",(GroupID, owner.get('displayName', 'Null'),owner.get('givenName', 'Null'), owner.get('surname', 'Null'), owner.get('jobTitle', 'Null'), owner.get('userPrincipalName', 'Null'), owner.get('mail', 'Null'), owner.get('mobilePhone', 'Null'), owner.get('officeLocation', 'Null'), sysdate))