簡體   English   中英

Google IOT Core 和 Raspberry Pi:錯誤:連接被拒絕:用戶名或密碼錯誤

[英]Google IOT Core and Raspberry Pi: Error: Connection Refused: Bad username or password

我按照下面的教程將我的樹莓派 3 連接到 Google IOT Core。 我在 Google 控制台上設置了 Google Core IOT 部分 OK,並遵循了樹莓派部分的所有步驟,但是,根據下面的錯誤消息,連接總是被拒絕。

error { Error: Connection refused: Bad username or password
at MqttClient._handleConnack (/home/pi/Desktop/Google-IoT- 
Device/node_modules/mqtt/lib/client.js:920:15)
at MqttClient._handlePacket (/home/pi/Desktop/Google-IoT- 
Device/node_modules/mqtt/lib/client.js:350:12)
at work (/home/pi/Desktop/Google-IoT- 
Device/node_modules/mqtt/lib/client.js:292:12)
at Writable.writable._write (/home/pi/Desktop/Google-IoT- 
Device/node_modules/mqtt/lib/client.js:302:5)
at doWrite (/home/pi/Desktop/Google-IoT- 
Device/node_modules/mqtt/node_modules/readable- 
stream/lib/_stream_writable.js:428:64)
at writeOrBuffer (/home/pi/Desktop/Google-IoT- 
Device/node_modules/mqtt/node_modules/readable- 
stream/lib/_stream_writable.js:417:5)
at Writable.write (/home/pi/Desktop/Google-IoT- 
Device/node_modules/mqtt/node_modules/readable- 
stream/lib/_stream_writable.js:334:11)
at TLSSocket.ondata (_stream_readable.js:639:20)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7) code: 4 }
close

教程鏈接: https ://hub.packtpub.com/build-google-cloud-iot-application/#comment-53421

這是我的 index.js 文件的頂部:

var fs = require('fs'); 
var jwt = require('jsonwebtoken'); 
var mqtt = require('mqtt'); 
var rpiDhtSensor = require('rpi-dht-sensor'); 

var dht = new rpiDhtSensor.DHT11(2); // `2` => GPIO2 

var projectId = 'nifty-*******-******'; 
var cloudRegion = 'us-central1'; 
var registryId = 'device-registry'; 
var deviceId = 'raspberrypi'; 

var mqttHost = 'mqtt.googleapis.com'; 
var mqttPort = 8883; 
var privateKeyFile = '../certs/rsa_private.pem'; 
var algorithm = 'RS256'; 
var messageType = 'state'; // or event 

var mqttClientId = 'projects/' + projectId + '/locations/' + cloudRegion + 
'/registries/' + registryId + '/devices/' + deviceId; 
var mqttTopic = '/devices/' + deviceId + '/' + messageType; 

var connectionArgs = { 
  host: mqttHost, 
  port: mqttPort, 
  clientId: mqttClientId, 
  username: 'unused', 
  password: createJwt(projectId, privateKeyFile, algorithm), 
  protocol: 'mqtts', 
  secureProtocol: 'TLSv1_2_method' 
}; 

本教程沒有說明如何下載 Google 根 CA 證書,所以我按照本教程進行操作: https ://raspberrypi.stackexchange.com/questions/76419/entrusted-certificates-installation

我還通過在 Google 上執行此操作檢查了連接路由是否正常,並且一切正常: https ://cloud.google.com/iot/docs/troubleshooting

projectID、registryID、deviceID 和 region 都檢查正確。

我相信它一定很簡單,但這讓我沮喪了一個星期。 我已經在互聯網上進行了搜索,但我嘗試過的結果都導致了同樣的錯誤。 有沒有人可以提供幫助?

需要三重檢查的事情:

  1. 您的項目 ID、注冊表和設備名稱都正確,大小寫和破折號與下划線都正確
  2. 您的 SSL 密鑰類型與注冊表中的算法和指定類型匹配。 也就是說,如果您有 RSA 密鑰,請確保它是 RSA 而不是在注冊表中指定 x509 的 RSA。
  3. 根證書是正確的......你鏈接的那個教程比它需要的要復雜得多。 只需運行: wget https://pki.google.com/roots.pem從 Google 獲取當前的 roots.pem。

不是向你扔另一個教程,但我實際上也剛剛發布了一篇博客文章,其中包含有關此內容的詳細信息,主要是因為其他教程要么有漏洞,要么信息陳舊。

另一條注意事項:我看到您正在使用state MQTT 主題發送,沒錯,但是在您列出的評論中event events 因此,如果您嘗試發送到event ,那也會失敗。

我遇到並解決了同樣的問題,減少了為密碼創建 json 令牌的到期時間

對我來說,這是一個簡單的疏忽,即等號 (=) 和區域名稱之間的空格。 代碼是:

之前(失敗)....

node cloudiot_mqtt_example_nodejs.js mqttDeviceDemo \
--projectId=myproject \
--cloudRegion= us-central1 \
--registryId=1234 \
--deviceId=test-device \
--privateKeyFile=./cert/rsa_private.pem \
--numMessages=25 \
--algorithm=RS256

之后(固定錯誤消息“錯誤錯誤:連接被拒絕:錯誤的用戶名或密碼) ....

--cloudRegion=us-central1 \

另外,請參閱此處的示例: https ://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/iot/mqtt_example

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM