Home > Software design >  Creating the necessary SAS-Key to communicate with the Azure IoT-Hub
Creating the necessary SAS-Key to communicate with the Azure IoT-Hub

Time:03-23

We are currently using the Azure IoT-Hub to receive data from our IoT devices. They are currently using the REST API to send their data, so they'll POST data to an address like https://<iothubname>.azure-devices.net/devices/<deviceID>/messages/events

with <iothubname> and <deviceID> replaced by their specific definitions. For Authorization I've added the field "Authorization" to the header and pasted the device's SAS token I've created using the Azure IoT Plugin for VS Code. This works so far, but I'll now like to create the Token on my own for further automation.

MS explains how to create a token on their site, but it doesn't work out for me (I've tried the python implementation).

For URI I'd use the one above, for key I'd use the key from the device in the IoT-Hub but I'm unsure what to use for policy_name. I tried the default "iothubowner" and the device name.

Furthermore the structure of the created SAS key isn't the same as the one created by the Azure IoT Plugin.

The first contains the parts sr, sig, se and skn, the later sr, sig and se

CodePudding user response:

If you're using the Python sample you linked to, here are the parameters you need to use:

  • URI: <iothubname>.azure-devices.net/devices/<deviceID>/messages/events?api-version=2020-03-13

Don't include the protocol, do include the API version

  • key: <primary/secondary key>
  • policy_name: None

You don't need a policy name when connecting as a device|

This will generate a correct SharedAccesSignature that you can use to POST to https://<iothubname>.azure-devices.net/devices/<deviceID>/messages/events?api-version=2020-03-13

  • Related