question

IainWhite-1972 avatar image
0 Votes"
IainWhite-1972 asked IainWhite-1972 edited

UnauthorizedError when trying to debug IoT Edge module (Node JS) locally using VS Code.

I have a couple of Node JS Modules that I'm now having problems locally debugging. Local debug was working a few weeks ago and I'm not sure what has changed.

Error message:
[UnauthorizedError: mqtt.js returned Failure on first connection (Not authorized): Connection refused: Not authorized error] {
transportError: Error: Failure on first connection (Not authorized): Connection refused: Not authorized
at C:\Users[Truncated]\node_modules\azure-iot-device-mqtt\dist\mqtt.js:235:104

I'm using VSCode.

Steps to replicate:
Command: Azure IoT Edge: Setup IoT Edge Simulator
Command: Azure IoT Edge: Start IoT Edge Simulator for Single Module
From Debug, select the Local Debug (Node.js) for the appropriate module.


Snippet of my code:
const Transport = require('azure-iot-device-mqtt').Mqtt;
const Client = require('azure-iot-device').ModuleClient;

     Client.fromEnvironment(Transport, (err, client) => {
       if (err) {
         throw err;
       } else {
         client.on('error', function (err) {
           throw err;
         });
        
         // connect to the Edge instance
         client.open(onConnect);
       }
     });

When I debug, I can see the connection string in module_client.js being set to:

HostName=[my-hub].azure-devices.net;GatewayHostName=localhost;DeviceId=[correct device id];ModuleId=target;SharedAccessKey=LIm[Truncated]k=
This ties in with the connection string I get from the portal - only difference being the "GatewayHostName=localhost;" which doesn't exist on the portal one.
Edit: I checked this again and the SharedAccessKey doesn't tie in with the ones (primary & secondary) in the portal. It's different. I could have sworn it was the same the first time I checked but I might be wrong. Should it be the SharedAccessKey from the portal. I tried overriding while in debug and it didn't seem to improve matters. Still getting the "Not authorised error".

Additional info: I have a few edge devices. I am having the same issue on both of them.

Anything else I can check? Any suggestions?

azure-iot-edge
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello anonymous user

What is the IoT Edge version you are using over here in this solution?


109235-image.png


0 Votes 0 ·
image.png (59.3 KiB)

Hi @SatishBoddu-MSFT, I'm using the 1.2.0 iotedge runtime on my device.

For node.js dependencies I'm using azure-iot-device version 1.17.4 and azure-iot-device-mqtt version 1.15.4.

0 Votes 0 ·

0 Answers