IoTHub-Client connect error: not authorized (MQTT)

YG 1 Reputation point
2022-03-22T14:41:58.77+00:00

Hey Guys,

I followed this tutorial: https://learn.microsoft.com/de-de/azure/iot-hub/iot-hub-raspberry-pi-kit-node-get-started

I cant get the Application running because of following error:

[Device] Using MQTT transport protocol  
[IoT Hub Client] Connect error:  
        mqtt.js returned Failure on first connection (Not authorized): Connection refused: Not authorized error  
  

185714-image.png

Sending from test message from VS Code works.

Can someone please tell me why, I dont have a clue :(

Thanks for your help!

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,157 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sander van de Velde | MVP 31,211 Reputation points MVP
    2022-03-22T17:28:47.673+00:00

    Hello ,

    this tutorial connects a Raspberry Pi to the Azure IoT Hub.

    Each azure IoT device has its own credentials.

    It complains about the authorization. Are you sure you passed the connection string:

    sudo node index.js '<YOUR AZURE IOT HUB DEVICE CONNECTION STRING>'

    Please check this.

    As an alternative, you can check this RPI simulation to get familiar with the code and connection string.

    Regarding the Device-to-Cloud message, this is actually the right experience!

    The device will send Cloud-to-Device messages.

    Azure IoT Hub supports several ways to communicate to the device:

    Direct methods only work when the device is connected at the same time as the direct method is called.

    Both desired properties and the cloud messages are 'stored' on the IoT hub until the device connects to the IoT Hub and consumes the device twin changes or the cloud messages.

    So, the cloud messages you already sent are probably still queued on the IoT Hub.

    For all three ways of C2D communication, extra local logic must be added on the device.


  2. chbeier 1,866 Reputation points
    2022-03-22T17:58:28.98+00:00

    Hi @YG ,
    I assume you created your device and used its connection string to start the application as mentioned in the tutorial

    sudo node index.js '<YOUR AZURE IOT HUB DEVICE CONNECTION STRING>'  
    

    The failure might occur when the connection string is not correct e.g., when the last characters get removed from the SharedAccessKey. Be careful to copy the entire string from the portal and put it in quotation marks.

    0 comments No comments