Search code examples
azureazure-iot-hubazure-iot-hub-device-management

Get iothub registered devices via REST API


Hello i am trying to fetch devices present/registered at IOTHUB via RESTAPI. But i am confused how to fetch all devices. I have read the documentation here : IoT Hub Service - Get Devices

But When i send a request i am getting an error

"Message": "ErrorCode:IotHubUnauthorizedAccess;Unauthorized", "ExceptionMessage": "Tracking ID:a795ee1f7ae04adfa600333e45e9aa09-G:5-TimeStamp:06/29/2020 14:32:56"

Is there any auth token to provide in order to get devices?


Solution

  • So for a quick start on this we can use Postman with Azure IoT Hub Query language..+ SAS token for authorization.

    Step 1: Generate the SAS token as said by Matthijs, Also we can quickly make use of Device Explorer tool Or Use this link to find the SetupDeviceExplorer.msi. Copy the generated SAS token fully.

    enter image description here

    Step 2: Construct the Query body.

    the POST query looks like this sample. See Registry Manager - Query Iot Hub

    POST https://IOTHUB.azure-devices.net/devices/query?api-version=2020-03-13

    {
      "query": "SELECT deviceId FROM devices"
    }
    

    enter image description here

    Step 3: Authorization use SAS token, and send the request to test it out.

    enter image description here

    A similar thread answered by RomanKiss can be read from Get all devices from IoT Azure Hub. Please let us know if you know further help!