Forum Discussion
nabi04
Apr 03, 2023Brass Contributor
How to get user's device info in MS Graph?
I am trying to determine what operating system a user is using upon login, whether Mac or Windows. Is there a query on getting it from MS Graph API?
nabi04
Apr 04, 2023Brass Contributor
Hi josequintino I have tried your suggestion but the response was blank:
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#directoryObjects",
"value": []
}
This probably means that I don't have any managed devices in Azure. I tried using this query:
https://graph.microsoft.com/v1.0/auditLogs/signIns
"value": [
{
"id": "xxxxx-xxxx-xxxxx",
"createdDateTime": "2023-03-16T10:31:57Z",
"userDisplayName": "xxxxx",
"userPrincipalName": "xxxxx",
"userId": "xxxx-xxxxx-xxxx",
"appId": "xxxx-xxxxx-xxxx",
"appDisplayName": "xxxxx",
"ipAddress": "xxxx.xxx.xx.xxx",
"clientAppUsed": "Browser",
"deviceDetail": {
"deviceId": "",
"displayName": "",
"operatingSystem": "Windows 10",
"browser": "Edge 111.0.1661",
"isCompliant": false,
"isManaged": false,
"trustType": ""
},
]
But I cannot query to fetch only the "operatingSystem". Do you know how can I parse through this JSON response?
josequintino
Apr 05, 2023MCT
Hi nabi04
Sure, to parse the JSON response and extract the operatingSystem property for each sign-in event, you can use a programming language like Python, JavaScript, or any language that supports JSON parsing. I'll provide examples for both Python and JavaScript.
Python example:
import json
response = '''
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#directoryObjects",
"value": [
{
"id": "xxxxx-xxxx-xxxxx",
"createdDateTime": "2023-03-16T10:31:57Z",
"userDisplayName": "xxxxx",
"userPrincipalName": "xxxxx",
"userId": "xxxx-xxxxx-xxxx",
"appId": "xxxx-xxxxx-xxxx",
"appDisplayName": "xxxxx",
"ipAddress": "xxxx.xxx.xx.xxx",
"clientAppUsed": "Browser",
"deviceDetail": {
"deviceId": "",
"displayName": "",
"operatingSystem": "Windows 10",
"browser": "Edge 111.0.1661",
"isCompliant": false,
"isManaged": false,
"trustType": ""
}
}
]
}
'''
data = json.loads(response)
for sign_in in data["value"]:
operating_system = sign_in["deviceDetail"]["operatingSystem"]
print(operating_system)
JavaScript example:
const response = `
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#directoryObjects",
"value": [
{
"id": "xxxxx-xxxx-xxxxx",
"createdDateTime": "2023-03-16T10:31:57Z",
"userDisplayName": "xxxxx",
"userPrincipalName": "xxxxx",
"userId": "xxxx-xxxxx-xxxx",
"appId": "xxxx-xxxxx-xxxx",
"appDisplayName": "xxxxx",
"ipAddress": "xxxx.xxx.xx.xxx",
"clientAppUsed": "Browser",
"deviceDetail": {
"deviceId": "",
"displayName": "",
"operatingSystem": "Windows 10",
"browser": "Edge 111.0.1661",
"isCompliant": false,
"isManaged": false,
"trustType": ""
}
}
]
}`;
const data = JSON.parse(response);
data.value.forEach((signIn) => {
const operatingSystem = signIn.deviceDetail.operatingSystem;
console.log(operatingSystem);
});
Both of these examples parse the JSON response and iterate over the sign-in events in the value array. They then extract the operatingSystem property from the deviceDetail object and print it.
Note that these examples assume you already have the JSON response as a string. In practice, you would fetch the data from the Microsoft Graph API using an HTTP library, such as requests in Python or fetch in JavaScript, and then parse the response accordingly.
Sure, to parse the JSON response and extract the operatingSystem property for each sign-in event, you can use a programming language like Python, JavaScript, or any language that supports JSON parsing. I'll provide examples for both Python and JavaScript.
Python example:
import json
response = '''
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#directoryObjects",
"value": [
{
"id": "xxxxx-xxxx-xxxxx",
"createdDateTime": "2023-03-16T10:31:57Z",
"userDisplayName": "xxxxx",
"userPrincipalName": "xxxxx",
"userId": "xxxx-xxxxx-xxxx",
"appId": "xxxx-xxxxx-xxxx",
"appDisplayName": "xxxxx",
"ipAddress": "xxxx.xxx.xx.xxx",
"clientAppUsed": "Browser",
"deviceDetail": {
"deviceId": "",
"displayName": "",
"operatingSystem": "Windows 10",
"browser": "Edge 111.0.1661",
"isCompliant": false,
"isManaged": false,
"trustType": ""
}
}
]
}
'''
data = json.loads(response)
for sign_in in data["value"]:
operating_system = sign_in["deviceDetail"]["operatingSystem"]
print(operating_system)
JavaScript example:
const response = `
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#directoryObjects",
"value": [
{
"id": "xxxxx-xxxx-xxxxx",
"createdDateTime": "2023-03-16T10:31:57Z",
"userDisplayName": "xxxxx",
"userPrincipalName": "xxxxx",
"userId": "xxxx-xxxxx-xxxx",
"appId": "xxxx-xxxxx-xxxx",
"appDisplayName": "xxxxx",
"ipAddress": "xxxx.xxx.xx.xxx",
"clientAppUsed": "Browser",
"deviceDetail": {
"deviceId": "",
"displayName": "",
"operatingSystem": "Windows 10",
"browser": "Edge 111.0.1661",
"isCompliant": false,
"isManaged": false,
"trustType": ""
}
}
]
}`;
const data = JSON.parse(response);
data.value.forEach((signIn) => {
const operatingSystem = signIn.deviceDetail.operatingSystem;
console.log(operatingSystem);
});
Both of these examples parse the JSON response and iterate over the sign-in events in the value array. They then extract the operatingSystem property from the deviceDetail object and print it.
Note that these examples assume you already have the JSON response as a string. In practice, you would fetch the data from the Microsoft Graph API using an HTTP library, such as requests in Python or fetch in JavaScript, and then parse the response accordingly.