Azure IoT Hub MQTT failure(Without SDK)

Stack Overflow Asked by govtham on November 5, 2020

I am trying to use Microsoft Azure IOT Hub(MQTT) to connect my java client device to IOT Hub(without SDK). I created device and SAS with TTL as required. I am using paho client to connect to IoT Hub.I am getting below error. SAS token i created from IoT Hub Tools with 5 hours of expiry time.
Not authorized to connect (5)
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(
at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(
at Source)

Below is the code i am trying

String deviceId = "mqqdirectdevice1";     
          String brokerUri = "ssl://xxxxx:8883";
          String clientId = deviceId;
          System.out.println( "Connecting to " + brokerUri +" as "+clientId);

          MqttAsyncClient client = null;
          try {           
              String sasToken="SharedAccessSignature";
              client = new MqttAsyncClient( brokerUri, clientId,new MemoryPersistence());
            if ( client != null ) {             
              MqttConnectOptions options = new MqttConnectOptions();                          
              client.setCallback( new AzureCallback() );             
             // options.setPassword(sasToken.toCharArray());
              IMqttToken token=client.connect( options );
              token.waitForCompletion(60 * 1000);
              System.out.println("Sent MQTT CONNECT packet was acknowledged");
              if ( client.isConnected() ) {
                System.out.println( "Success!" );
              } else {
                System.out.println( "Could not connect to Azure IoT hub, timed-out" );
          } catch ( MqttException) {
          } finally {
            if ( client != null ) {
              try {
              } catch ( MqttException ignore ) {}

One Answer

In your example, you use the following username:

The format is slightly different, you are currently missing a forward slash before the question mark. The correct format is:

I tested it, and the MQTT connection can't be made with the username in an incorrect format.

Answered by Matthijs van der Veer on November 5, 2020

Add your own answers!

Related Questions

Choice on a list affects the following duplicated list

0  Asked on December 11, 2021 by mrtsm


How can I change server in spring boot application?

4  Asked on December 11, 2021 by vikram-shekhawat


How to modify ggplot2 legend keys?

3  Asked on December 11, 2021 by philippe-massicotte


Split JSON to multiple Objects

2  Asked on December 11, 2021 by marcio-lino


Angular/Typescript Text with routerLink

2  Asked on December 11, 2021 by dotnet


How to use multiple factors in na.aggregate() from zoo package

1  Asked on December 11, 2021 by shadrack-kibet


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP