device client register method fails with CredentialError caused by UnauthorizedError when attempting to connect to a device on iot central using python sdk.

Varun Venkatesh 1 Reputation point
2020-11-06T19:01:28.363+00:00

Software versions:
Python==3.7.8
azure-iot-device==2.3.0

We are attempting to check if we can push telemetry data onto a device created in Azure IoT Central by following the tutorial:

https://learn.microsoft.com/en-us/azure/iot-central/core/tutorial-connect-device-python

On running the environmental_sensor.py that we created, we were hit with the following message:

# python3 env_sensor.py  
Device could not connect  

We then modified the script to be a sync version of the same using:

https://github.com/Azure/azure-iot-sdk-python/blob/master/azure-iot-device/samples/sync-samples/provision_symmetric_key.py

as reference.

Upon running the environmental_sensor.py, we encountered the following error:

~# python3 sync_env_sensor.py  
Traceback (most recent call last):  
  File "/usr/lib/python3.7/site-packages/azure/iot/device/provisioning/provisioning_device_client.py", line 25, in handle_result  
    return callback.wait_for_completion()  
  File "/usr/lib/python3.7/site-packages/azure/iot/device/common/evented_callback.py", line 70, in wait_for_completion  
    raise self.exception  
azure.iot.device.common.transport_exceptions.UnauthorizedError: UnauthorizedError('Connection Refused: not authorised.')  
  
The above exception was the direct cause of the following exception:  
  
Traceback (most recent call last):  
  File "sync_env_sensor.py", line 25, in <module>  
    registration_result = provisioning_device_client.register()  
  File "/usr/lib/python3.7/site-packages/azure/iot/device/provisioning/provisioning_device_client.py", line 73, in register  
    self._enable_responses()  
  File "/usr/lib/python3.7/site-packages/azure/iot/device/provisioning/provisioning_device_client.py", line 96, in _enable_responses  
    handle_result(subscription_complete)  
  File "/usr/lib/python3.7/site-packages/azure/iot/device/provisioning/provisioning_device_client.py", line 35, in handle_result  
    raise exceptions.CredentialError(message="Credentials invalid, could not connect", cause=e)  
azure.iot.device.exceptions.CredentialError: CredentialError('Credentials invalid, could not connect') caused by UnauthorizedError('Connection Refused: not authorised.')  
  

Any assistance would be appreciated.

Azure IoT Central
Azure IoT Central
An Azure hosted internet of things (IoT) application platform.
353 questions
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
208 questions
{count} votes

1 answer

Sort by: Most helpful
  1. QuantumCache 20,031 Reputation points
    2020-12-07T19:04:20.347+00:00

    Hello @Varun Venkatesh , Posting the ongoing investigation from the product team on this issue,

    Unfortunately, we haven't been able to reproduce this yet, so let's keep investigating.

    Could you please repeat the tutorial from the beginning and share screen shots (or video record) your steps, particularly when you get and set your connection env. variables? You can blur over your credentials if you'd prefer.

             set IOTHUB_DEVICE_SECURITY_TYPE=DPS  
             set IOTHUB_DEVICE_DPS_ID_SCOPE=<The ID scope you made a note of previously>  
             set IOTHUB_DEVICE_DPS_DEVICE_ID=sample-device-01  
             set IOTHUB_DEVICE_DPS_DEVICE_KEY=<The group primary key you made a note of previously>  
             set IOTHUB_DEVICE_DPS_ENDPOINT=global.azure-devices-provisioning.net  
    

    Another thing to try is to just temporarily hard code these environment variables into the sample file to ensure there's nothing wrong with your environment or how you're setting these.

    Let us know when you've tried again and we'll take a look at the screen shots/video and go from there.