将gcloud连接到MQTT桥时遇到问题

问题描述 投票:0回答:1

我目前正在尝试将gcloud IoT项目连接到MQTT桥,以便能够发布遥测数据。我正在使用Mosquitto,并试图在我在gcloud计算引擎上创建的VM实例上运行mosquitto_pub命令。我还创建了一个防火墙规则来打开优先级为0的端口8883。我正在提交以下命令(我删除了所使用的JWT并将其放在代码块中)

mosquitto_pub \
--host mqtt.googleapis.com \
--port 8883 \
--id projects/telemetry-268916/locations/us-central1/registries/iotcore-registry-telemetry/devices/esp32 \
--username unused \
--pw "<my-JWT.>" \
--cafile ./roots.pem \
--tls-version tlsv1.2 \
--protocol-version mqttv311 \
--debug \
--qos 1 \
--topic /devices/esp32/events \
--message "Hello MQTT"

当我运行此命令^时,出现以下错误:

Client projects/telemetry-268916/locations/us-central1/registries/iotcore-registry telemetry/devices/esp32 sending CONNECT
Client projects/telemetry-268916/locations/us-central1/registries/iotcore-registry-telemetry/devices/esp32 received CONNACK
Connection Refused: not authorised.
Error: The connection was refused.

我到处都是,找不到这个问题的解决方案。我正在使用根证书并指定CA文件,所以idk发生了什么-帮助!

mqtt iot gcloud google-cloud-pubsub mosquitto
1个回答
0
投票

我怀疑您的JWT到期日过长(> = 24h)。

[Google Cloud IoT的MQTT网关(文档:the maximum lifetime of a token

要求JWT为<= 24h

PROJECT=
REGISTRY=
REGION=
DEVICE=

CLIENT="projects/${PROJECT}/locations/${REGION}/registries/${REGISTRY}/devices/${DEVICE}"
TOPIC="/devices/${DEVICE}/events"

# Using a JWT generator; expiry=24h
PASSWORD=$(\
  go-jwt \
  --project=${PROJECT} \
  --private_key=${KEY} \
  --expiry=24h)

docker run \
--interactive --tty \
--volume=${PWD}/roots.pem:/roots.pem \
eclipse-mosquitto:1.6.8 mosquitto_pub \
  -h mqtt.googleapis.com -p 8883 \
  -i ${CLIENT} \
  -u unused -P ${PASSWORD} \
  -t ${TOPIC} \
  -m "Hello Freddie!" \
  --cafile /roots.pem \
  --debug \
  --qos 1 \
  --tls-version tlsv1.2 \
  --protocol-version mqttv311
sending CONNECT
received CONNACK (0)
sending PUBLISH (d0, q1, r0, m1, '/devices/.../events', ... (14 bytes))
received PUBACK (Mid: 1, RC:0)
sending DISCONNECT

# Using a JWT generator; expiry=25h
PASSWORD=$(\
  go-jwt \
  --project=${PROJECT} \
  --private_key=${KEY} \
  --expiry=25h)

docker run \
--interactive --tty \
--volume=${PWD}/roots.pem:/roots.pem \
eclipse-mosquitto:1.6.8 mosquitto_pub \
  -h mqtt.googleapis.com -p 8883 \
  -i ${CLIENT} \
  -u unused -P ${PASSWORD} \
  -t ${TOPIC}
  -m "Hello Freddie!" \
  --cafile /roots.pem \
  --debug \
  --qos 1 \
  --tls-version tlsv1.2 \
  --protocol-version mqttv311
CONNECT
CONNACK (4)
Connection error: Connection Refused: bad user name or password.
DISCONNECT
© www.soinside.com 2019 - 2024. All rights reserved.