title: Mqtt connection failed with exception 'Not authorized to connect (5)' android
body: <p>Hi I am trying to connect to mqtt with my android device that I just registered but it I keep getting the error  </p>
  
  <pre><code>Not authorized to connect (5)
  </code></pre>
  
  <p>This is some of my code</p>
  
  <pre><code>        client = new MqttAndroidClient(context, connectionURI, clientID);
          client.setCallback(callbacks);
  
          String username = IOT_DEVICE_USERNAME;
          char[] password = this.getAuthorizationToken().toCharArray();
  
          MqttConnectOptions options = new MqttConnectOptions();
          options.setCleanSession(true);
          options.setUserName(username);
          options.setPassword(password);
  
          Log.d(TAG, "Connecting to server: " + connectionURI);
          try {
              // connect
              return client.connect(options, context, listener);
          } catch (MqttException e) {
              Log.e(TAG, "Exception caught while attempting to connect to server", e.getCause());
              throw e;
          }
  </code></pre>
  
  <p>where <strong>IOT_DEVICE_USERNAME</strong> is a string <strong>"use-token-auth"</strong>
  and <strong>connectionUri</strong> is <strong>"tcp://&lt;"organization">.messaging.internetofthings.ibmcloud.com:8883"</strong>  </p>
  
  <p>Something else that is interesting is that right before this error I get this log  </p>
  
  <pre><code>Unregister alarmreceiver to MqttServiced:&lt;"organisation"&gt;:&lt;"DeviceType"&gt;:&lt;"deviceId"&gt;
  </code></pre>
  
  <p>Here is the stackTrace of the error</p>
  
  <pre><code>07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err: Not authorized to connect (5)
  07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28)
  07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:988)
  07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:140)
  07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
  07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
  07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
  07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  07-06 12:27:24.214 9009-9009/wibicom.wibeacon3 W/System.err:     at java.lang.Thread.run(Thread.java:818)
  </code></pre>