question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Authenticating `IoTHubRegistryManager` using the `from_token_credential` constructor

See original GitHub issue

Problem

  • Hi azure team; we have a need to manage several IoT hubs; sending cloud to device messages for eg. So far, we have been using the from_connection_string constructor to do this. However this is a bit clunky as it requires us to have the connection string encoded as a secret for each of the IoT hubs.

  • We are instead hoping to use from_token_credential to achieve authentication to all the IoT hubs within a subscription.

Attempt and error faced

We are following the instructions as given here to try out C2D messaging for a simulated device; the only difference being

# This line is removed
registry_manager = IoTHubRegistryManager(CONNECTION_STRING)

# This is added
from azure.identity import EnvironmentCredential
credential = EnvironmentCredential()
registry_manager = IoTHubRegistryManager.from_token_credential(
            url="azure-simlab1.azure-devices.net",
            token_credential=credential.get_token(os.environ.get("scopes")),
        ) 

Upon running the command; it either

  • Throws when an invalid scope is passed;
  • Or just hangs; the simulated device does not receive a message.
  • It works perfectly fine if we switch back to the connection string method.

Question

  • What is the correct scope for such a request?
  • Is the EnvironmentCredential the correct approach?

Thanks! Sahil

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:22 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
sgoyal1012commented, Aug 3, 2021

@vishnureddy17 that worked! Thanks for the quick turnaround, really appreciate it 😃

Here is the code that worked

credential = EnvironmentCredential()
registry_manager = IoTHubRegistryManager.from_token_credential(
    url="azure-simlab1.azure-devices.net",
    token_credential=credential,
)
0reactions
vishnureddy17commented, Aug 3, 2021

@sgoyal1012 , if you update your azure-iot-hub to version 2.5.0, you should be able to send the messages. Please let me know if this resolves it, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

azure.iot.hub.IoTHubRegistryManager class | Microsoft Learn
Creates a module identity for a device on IoTHub using X509 authentication. delete_device. Deletes a device identity from IoTHub. delete_module. Deletes a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found