eclipse-paho / eclipse-paho/paho.mqtt.python

Failed to connect: Not authorized. loop_forever() will retry connection

Open
#858 1 comment 0 reactions 0 assignees View on GitHub
Status: Available
Dominant language
Python
Stars
2.4k
Forks
742
Avg merge
12d 48m
Merged PRs (30d)
1

Description

# Prerequisites

*Note: You may remove this section prior to submitting your question.*

A small team of volunteers monitors issues; this is the same team that develops the library. Whilst we are keen to help,
there are often better places to ask questions, including:

- [Stack Overflow](https://stackoverflow.com/questions/tagged/mqtt) - well written questions are generally answered
within a day. Please use the tags MQTT, Python and Paho.
- [Reddit](https://www.reddit.com/r/MQTT/) - great for questions requiring discussion.
- [MQTT Google Group](https://groups.google.com/g/mqtt) - fairly quiet but questions about the protocol are generally answered quickly.
- [Eclipse paho-dev mailing list](https://dev.eclipse.org/mailman/listinfo/paho-dev) - for general discussion about the paho project.

Prior to asking a question here, please:

- [ ] Search the resources mentioned above (it's likely someone else has asked the same question)
- [ ] Read the [readme](https://github.com/eclipse/paho.mqtt.python/blob/master/README.rst) (especially the
"Known limitations" section) and look at the [examples](https://github.com/eclipse/paho.mqtt.python/tree/master/examples).
- [ ] Search through the [project issues](https://github.com/eclipse/paho.mqtt.python/issues).
- [ ] Confirm that you are using the latest release of the library.
- [ ] Ensure your question is specific to this project; consider using another tool (e.g. [MQTTX](https://mqttx.app/) / [mosquitto_sub](https://mosquitto.org/man/mosquitto_sub-1.html))
to test your assumptions.

# Question

(1) make account file
cd /etc/mosquitto
mosquitto_passwd -c .account userid
Password: passwd
Reenter password: passwd

(2) /etc/mosquitto/mosquitto.conf
.....
password_file /etc/mosquitto/.account
.....

(3) source code
import time
import paho.mqtt.client as mqtt

MQTT_SERVER = "127.0.0.1"
MQTT_PORT = 1883
MQTT_TOPIC = "wind"

def on_connect(client, userdata, flags, reason_code, properties):
if reason_code.is_failure:
print(f"Failed to connect: {reason_code}. loop_forever() will retry connection")
else:
# we should always subscribe from on_connect callback to be sure
# our subscribed is persisted across reconnections.
#client.subscribe("$SYS/#")
client.subscribe(MQTT_TOPIC)

.......

client_userdata = 1

mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)

......
mqttc.on_connect = on_connect
......

username = "userid"
password = "passwd"

mqttc.username_pw_set(username, password)
mqttc.user_data_set([])
mqttc.connect(MQTT_SERVER, MQTT_PORT)

.....
mqttc.loop_forever()

# Result
Failed to connect: Not authorized. loop_forever() will retry connection

# Environment
*It's often helpful for us to know how you are using the library so please provide:*

* Python version:
* Library version:
* Operating system (including version):
* MQTT server (name, version, configuration, hosting details):

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.