Azure DPS - azure-iot-sdk-python client register() error in sample provision_symmetric_key.py - "Unexpected failure"

Jeffrey Beman 1 Reputation point
2022-12-02T18:53:36.933+00:00

I'm trying to use the python azure sdk for the DPS service, I only have one device, the environment variables are set correctly, why am I getting an "Unexpected failure" error?

Below is a snippet from the sample code from GitHub \azure-iot-sdk-python\samples\sync-samples\provision_symmetric_key.py--I'm using Python 3.11 for Windows:

provisioning_host = os.getenv("PROVISIONING_HOST")
id_scope = os.getenv("PROVISIONING_IDSCOPE")
registration_id = os.getenv("PROVISIONING_REGISTRATION_ID")
symmetric_key = os.getenv("PROVISIONING_SYMMETRIC_KEY")

Note the environment variables are all set correctly.

>> provisioning_host

'global.azure-devices-provisioning.net'

>> id_scope

'0ne00883FF1'

>> registration_id

'myDevice'

>> symmetric_key

'HostName=HubMsgDPS.azure-devices-provisioning.net;SharedAccessKeyName=provisioningserviceowner;SharedAccessKey=AVB/************************W/4sc='

provisioning_device_client = ProvisioningDeviceClient.create_from_symmetric_key(
provisioning_host=provisioning_host,
registration_id=registration_id,
id_scope=id_scope,
symmetric_key=symmetric_key,
)

registration_result = provisioning_device_client.register()

Exception has occurred: ClientError
Unexpected failure

The above exception was the direct cause of the following exception:

File ".\azure-iot-sdk-python\samples\sync-samples\provision_symmetric_key.py", line 24, in \

Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
222 questions
{count} votes

2 answers

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 15,801 Reputation points Microsoft Employee
    2022-12-15T21:59:17.413+00:00

    Hello @Jeffrey Beman ,

    Greetings! I am checking to see if you are still encountering the issue. I have tested the Python DPS SDK from this GitHub resource Provision Symmetric key and could successfully register the device. Please find the below output generated when I executed the Python code

    271212-image.png

    Here is the IoT device created on the IoT Hub from the SDK code sample

    271177-image.png

    I have noticed that the symmetric key you provided in the code is obtained from the Shared Access policies of your DPS. However, the symmetric key has to be obtained from the individual enrollment created under DPS. Please refer steps 6 through 10 of the Create a device enrollment QuickStart guide to capture and use the correct key. Below is the snapshot from where we can get the key

    271203-image.png

    Please give this a try and let us know if you still run into any issue.

    ----------

    Kindly accept answer or upvote if the response is helpful so that it would benefit other community members facing the same issue. I highly appreciate your contribution to the community.

    0 comments No comments

  2. QuantumCache 20,271 Reputation points
    2023-01-25T15:27:26.83+00:00

    Hello @Jeffrey Beman ,

    In addition to above response from LeelaRajesh, please look the DPS troubleshooting page as well to get some help on these errors.

    Troubleshooting network issues with unassigned devices:

    Verify the values of the parameters passed to the ProvisioningDeviceClient.create_from_symmetric_key()

    match the values configured in the DPS. We can also check for duplicate registration_ID already registered with DPS?

    User's image

    Troubleshooting with Azure IoT Hub Device Provisioning Service

    Please leave your comment in the below section so that we could help you further.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.