Error: not allowed to connect when using Paho Android Service

I am trying to post a message to the IBM BlueMix IoTF service using the Eclipse Paho MQTT Android client. When I call a method MqttAndroidClient#connect(), I get an error in the callback onFailure()-Not authorized to connect (5)

My connection code (this code is triggered from the OnClick event with a single button) -

    mqtt = new MqttAndroidClient(
            MainActivity.this,
            "tcp://<org-id>.messaging.internetofthings.ibmcloud.com:1883",
            "d:<org-id>:MQTTdevices:watson-android"
    );

    MqttConnectOptions options = new MqttConnectOptions();

    options.setCleanSession(true);

    options.setUserName("use-token-auth");
    options.setPassword("<token>".toCharArray());

    Log.i(TAG, "attempting connection");

    try {
        mqtt.connect(options, new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                Log.i(TAG, "connected to mqtt");
                isconneted = true;
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable e) {
                Log.e(TAG, "mqtt connect failed", e);
                isconneted = false;
            }
        });
    } catch (MqttException e) {
        Log.e(TAG, "mqtt connect failed", e);
    }

Using the same credentials, I can connect to IoTF and post messages using mosquitto_pubthe command line client. How to get rid of this error?

+4
source share

Source: https://habr.com/ru/post/1627007/


All Articles