Search code examples
javamicrosoft-graph-apimicrosoft-graph-sdksazure-java-sdk

Retrieve all Managed Devices using Java (with Microsoft Graph API SDK)


I would like to retrieve all devices managed by Intune (managed devices) using the Microsoft Graph Java SDK. I have created the app in Microsoft Azure and given the appropriate API permissions:

API Permissions

The following code creates a graphClient object and a method that retrieves all managed devices.

    @Service
public class AzureServiceDefault implements AzureService
    {
        private static final String CLIENT_ID = "XXXXXXXXXXXXXXXXXXXXXXXX";
        private static final List<String> SCOPES = Arrays.asList(new String[]{"https://graph.microsoft.com/.default"});
        private static final String TENANT = "XXXXXXXXXXXXXXXXXXXXXXXX";
        private static final String CLIENT_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXX";
        ClientCredentialProvider authProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT, NationalCloud.Global);
        IGraphServiceClient graphClient;
    
        public AzureServiceDefault()
        {
            graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();
    
        } 

    @Override
        public List<IntuneDevice> getManagedDevices()
        {
            IManagedDeviceCollectionRequestBuilder managedDeviceRequestBuilder;
            IDeviceManagementRequestBuilder builder = graphClient.deviceManagement();
            IDeviceManagementRequest managedDevicesRequest = builder.buildRequest();
            List<ManagedDevice> managedDevices = new ArrayList<>();
            List<IntuneDevice> allManagedDevices = new ArrayList<>();
            
            do {
                try {
                    DeviceManagement deviceManagement = managedDevicesRequest.get();
                    ManagedDeviceCollectionPage managedDevicesCollectionPage = deviceManagement.managedDevices;
                    
                    //Process items in the response
                    managedDevices.addAll(managedDevicesCollectionPage.getCurrentPage());
                    managedDevices.stream().forEach((device) -> allManagedDevices.add(new IntuneDevice(device.id, 
                                                                                               device.userId,
                                                                                               device.deviceName,
                                                                                               device.managedDeviceOwnerType.toString(),
                                                                                               device.operatingSystem,
                                                                                               device.osVersion,
                                                                                               device.complianceState.toString(),
                                                                                               device.azureADRegistered,
                                                                                               device.azureADDeviceId,
                                                                                               device.userPrincipalName,
                                                                                               device.model,
                                                                                               device.manufacturer,
                                                                                               device.serialNumber)));
                
                
                
                    //Build the request for the next page, if there is one
                    managedDeviceRequestBuilder = managedDevicesCollectionPage.getNextPage();
                    if (managedDeviceRequestBuilder == null)
                    {
                        managedDevicesRequest = null;
                    }
                    else
                    {
                        managedDevicesRequest = (IDeviceManagementRequest) managedDeviceRequestBuilder.buildRequest();
                    }
                }
                catch(ClientException ex)
                {
                    ex.printStackTrace();
                    managedDevicesRequest = null;
                }
    
            } while (managedDevicesRequest != null);
            
            return allManagedDevices;
 

           }
    }

The problem is that the variable managedDevices turns out to be null and this is the error message:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/] threw exception [Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "com.microsoft.graph.requests.extensions.ManagedDeviceCollectionPage.getCurrentPage()" because "managedDevicesCollectionPage" is null] with root cause
java.lang.NullPointerException: Cannot invoke "com.microsoft.graph.requests.extensions.ManagedDeviceCollectionPage.getCurrentPage()" because "managedDevicesCollectionPage" is null

What do I need to change to make this code work? I am succesfully able to retrieve all users in Azure AD, but I am having difficulties getting data from Intune/Endpoint Manager. Do I need to make changes to the SCOPES?

It should be possible to retrieve all managed devices as the REST API for it is https://graph.microsoft.com/v1.0/deviceManagement/managedDevices

Thanks for your help


Solution

  • This MS Graph API does not support application permissions, so you couldn't list managedDevices with ClientCredentialProvider. ClientCredentialProvider is based on client credential flow that requires application permission.

    enter image description here

    You could use AuthorizationCodeProvider to get the list. And follow this to get AUTHORIZATION_CODE first.

        String CLIENT_ID = "xxxxxx";
        List<String> SCOPES = Arrays.asList(new String[] { "https://graph.microsoft.com/.default" });
        String CLIENT_SECRET = "xxxxxx";
        String TENANT = "xxxxxx";
        String AUTHORIZATION_CODE = "";
        String REDIRECT_URL = "xxxxxx";
    
        AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(CLIENT_ID, SCOPES, AUTHORIZATION_CODE,
                REDIRECT_URL, NationalCloud.Global, TENANT, CLIENT_SECRET);
    
        IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();
    
        IManagedDeviceCollectionPage managedDeviceCollectionPage = graphClient.deviceManagement().managedDevices().buildRequest().get();
        List<ManagedDevice> managedDeviceList = managedDeviceCollectionPage.getCurrentPage();