{"id":11574,"date":"2024-10-24T08:55:45","date_gmt":"2024-10-24T08:55:45","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11574"},"modified":"2024-10-24T08:55:45","modified_gmt":"2024-10-24T08:55:45","slug":"retrieve-azure-iot-connection-string-using-python","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/qa-automation\/retrieve-azure-iot-connection-string-using-python","title":{"rendered":"Retrieve Azure IoT Connection String Using Python"},"content":{"rendered":"<p>Here is the Python code that retrieves the device connection string:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">from azure.mgmt.iothub import IotHubClient\r\nfrom azure.identity import DefaultAzureCredential\r\nfrom azure.mgmt.iothub.models import SharedAccessSignatureAuthorizationRule\r\n\r\n# Subscription ID and resource group\r\nsubscription_id = 'ABCD1234'  # Your subscription ID\r\nresource_group = 'my-resource-group'\r\niot_hub_name = 'my-iot-hub'\r\ndevice_id = 'MY_DEVICE'\r\n\r\n# Use DefaultAzureCredential for authentication\r\ncredential = DefaultAzureCredential()\r\n\r\n# Initialize IoT Hub client\r\niot_hub_client = IotHubClient(credential, subscription_id)\r\n\r\n# Retrieve the primary connection string for the device\r\ndef get_device_connection_string():\r\n    # Get the device identity\r\n    device = iot_hub_client.iot_hub_resource.get_device(resource_group, iot_hub_name, device_id)\r\n\r\n    # Get the IoT Hub keys\r\n    keys = iot_hub_client.iot_hub_resource.list_keys(resource_group, iot_hub_name)\r\n\r\n    # Construct connection string\r\n    if keys:\r\n        for key in keys:\r\n            if key.rights == \"RegistryWrite\":\r\n                connection_string = f\"HostName={iot_hub_name}.azure-devices.net;DeviceId={device_id};SharedAccessKey={key.primary_key}\"\r\n                return connection_string\r\n\r\n# Retrieve connection string for the device\r\nconn_str = get_device_connection_string()\r\n\r\nprint(f\"Connection string for device {device_id}: {conn_str}\")\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is the Python code that retrieves the device connection string: from azure.mgmt.iothub import IotHubClient from azure.identity import DefaultAzureCredential from azure.mgmt.iothub.models import SharedAccessSignatureAuthorizationRule # Subscription ID and resource group subscription_id = &#8216;ABCD1234&#8217; # Your subscription ID resource_group = &#8216;my-resource-group&#8217; iot_hub_name = &#8216;my-iot-hub&#8217; device_id = &#8216;MY_DEVICE&#8217; # Use DefaultAzureCredential for authentication credential = DefaultAzureCredential() # Initialize [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11575,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[24],"tags":[],"class_list":["post-11574","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-qa-automation"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11574"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11574"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11574\/revisions"}],"predecessor-version":[{"id":11576,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11574\/revisions\/11576"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11575"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}