Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 tasks
kkakji opened this issue Aug 19, 2024 · 0 comments
Open
5 tasks
Labels
Status: Available No one has claimed responsibility for resolving this issue.

Comments

@kkakji
Copy link

kkakji commented Aug 19, 2024

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 - well written questions are generally answered
    within a day. Please use the tags MQTT, Python and Paho.
  • Reddit - great for questions requiring discussion.
  • MQTT Google Group - fairly quiet but questions about the protocol are generally answered quickly.
  • Eclipse paho-dev mailing list - 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 (especially the
    "Known limitations" section) and look at the examples.
  • Search through the project 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 / mosquitto_sub)
    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):
@github-actions github-actions bot added the Status: Available No one has claimed responsibility for resolving this issue. label Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Available No one has claimed responsibility for resolving this issue.
Projects
None yet
Development

No branches or pull requests

1 participant