jithin joseph
jithin joseph

Reputation: 176

Not authorized to connect (5) - MQTT google cloud IOT

I tried connecting to Google cloud IOT MQTT Brocker. I am getting Not authorized to connect (5) as Exception in Java client.

The device_id and other details are correct.The public key set for the device in the cloud iot.

using the example code for the below repo. https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/iot/api-client/mqtt_example

Upvotes: 4

Views: 3989

Answers (2)

Sian En Ooi
Sian En Ooi

Reputation: 61

Another point to look at is the configured GCP IoT Core cloud region.

Had similar problem with this. I configured GCP IoT Core cloud region as "asia-east1" while on the client application side, its default cloud region is "us-central1".

If the cloud region is not configured correctly, the python/NodeJS IoT Core examples (mqtt, http, end2end) would return errors such as

Creating JWT using RS256 from private key file rsa_private.pem
Publishing message 1/100: 'test/test_1-payload-1'
('on_connect', 'Connection Refused: not authorised.')
('on_disconnect', '5: The connection was refused.')

To solve this, just pass the correct cloud region parameter to the command --cloud_region=asia-east1

Example:

python cloudiot_mqtt_example.py --project_id=project_id --registry_id=registry_id --device_id=device_id --private_key_file=rsa_private.pem --algorithm=RS256 --cloud_region=asia-east1

Upvotes: 5

Gabe Weiss
Gabe Weiss

Reputation: 3342

Couple things to check:

1) Are you sure the format of the SSL key matches what you registered? RS256 vs. RS256 with X509, etc?

2) Did you setup the TLS on the mqtt client and grab the Google root certificate?

2a) >=TLS 1.2?

3) Verify the JWT has the correct 'aud' value (project-id, not project-name), and that it's got a proper issue and expiry time?

Upvotes: 0

Related Questions