Home > OS >  Access to read module twin data via Azure IoT Hub's APIs?
Access to read module twin data via Azure IoT Hub's APIs?

Time:01-05

I'd like to be able to pull an IoT Edge device's module twin via an HTTPS GET request just like the Azure Portal does. In the portal, I see this firing...

https://my-hub.azure-devices.net/twins/my-device/modules/my-module?api-version=2020-09-30&_=164130947416

...and returning JSON. I'd like to do the same outside of the portal (like in Postman or something).

Obviously, by being logged in to the portal, I have credentials that permit that.

I've tried creating a Shared Access Signature and using it as a header like so...

"Authorization": "SharedAccessSignature sr=my-hub.azure-devices.net&sig=...

...but I get back ErrorCode:IotHubUnauthorizedAccess;Unauthorized

Am I missing something? Maybe a step where I use that SAS to get some additional credential or something?

Or maybe I can't create a SAS that works with any device-id?

In the end, I'd just like to be able to GET the reported properties for a module twin from any of my IoT Hub devices. No setting, no subscribing.

CodePudding user response:

The API you're referring to is documented here. You're doing the right thing, but perhaps your method of creating a Shared Access Signature isn't working. One easy way to create a valid SAS token is by using the az cli command: az iot hub generate-sas-token -n <iot-hub-name>. By default it will create a token for the iothubowner policy, which should give you access to the module twin.

I used Postman, with the Authorization header as you mentioned to check the twin of my device $edgeHub module:

https://<my-iot-hub>.azure-devices.net/twins/<device-id>/modules/$edgeHub?api-version=2020-05-31-preview
  •  Tags:  
  • Related