# Client is not authorized to connect

**URL:** <https://community.hivemq.com/t/client-is-not-authorized-to-connect/856>\
**Category:** HiveMQ Cloud\
**Created:** [December 3, 2021, 5:32am UTC](https://community.hivemq.com/t/client-is-not-authorized-to-connect/856 "2021-12-03T05:32:17Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![chaiyasitr](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@chaiyasitr](https://community.hivemq.com/u/chaiyasitr)\
**Post date:** [February 21, 2022, 10:23am UTC](https://community.hivemq.com/t/client-is-not-authorized-to-connect/856/12 "2022-02-21T10:23:51Z")

</div>

@Daria_H  
Thank you for your supprt.

Below is information you request.

1. This picture to confirmed that my cluster work for web socket port 8884.  

2. Here is the source code that I used for evaluate. As you can see that it is very easily to switch between HiveMQ Cloud cluster & [http://test.mosquitto.org:8885](http://test.mosquitto.org:8885/) by just comment out `#define USE_HIVEMQ_CLOUD`

> #define USE\_HIVEMQ\_CLOUD
> 
> #ifdef USE\_HIVEMQ\_CLOUD  
> #define MQTT\_BROKER “a02edcfce43443e09599dbb82cbc2672.s1.eu.hivemq.cloud”  
> #define MQTT\_PORT (8883)  
> #define MQTT\_USER\_NAME “TestDevice001”  
> #define MQTT\_PASSWORD “xxxxxxxx”
> 
> ```
> char* rootCABuff = "-----BEGIN CERTIFICATE-----\n" \
> "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
> "-----END CERTIFICATE-----\n";
> 
> ```
> 
> #else  
> #define MQTT\_BROKER “[test.mosquitto.org](http://test.mosquitto.org)”  
> #define MQTT\_PORT (8885)  
> #define MQTT\_USER\_NAME “rw”  
> #define MQTT\_PASSWORD “readwrite”
> 
> ```
> char* rootCABuff = "-----BEGIN CERTIFICATE-----\n" \
> "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
> "-----END CERTIFICATE-----\n";
> 
> ```
> 
> #endif
> 
> char\* address = MQTT\_BROKER;  
> char \* username = MQTT\_USER\_NAME;  
> char \* password = MQTT\_PASSWORD;
> 
> NetworkInit(&network);  
> network.rootCA = (char\*)rootCABuff;  
> network.clientCA = NULL;  
> network.private\_key = NULL;  
> network.use\_ssl = 1;  
> MQTTClientInit(&client, &network, 30000, sendbuf, sizeof(sendbuf), readbuf,sizeof(readbuf));
> 
> mqtt\_printf(MQTT\_INFO, “Connect Network "%s"”, address);  
> while ((rc = NetworkConnect(&network, address, MQTT\_PORT)) != 0){  
> mqtt\_printf(MQTT\_INFO, “Return code from network connect is %d\n”, rc);  
> vTaskDelay(1000 / portTICK\_PERIOD\_MS);  
> }  
> mqtt\_printf(MQTT\_INFO, “"%s" Connected”, address);
> 
> connectData.username.cstring = username;  
> connectData.password.cstring = password;  
> connectData.MQTTVersion = 4; // 4 = Version 3.1.1  
> connectData.clientID.cstring = “Device001”;  
> connectData.cleansession = 1;
> 
> mqtt\_printf(MQTT\_INFO, “Start MQTT connection”);  
> while ((rc = MQTTConnect(&client, &connectData)) != 0){  
> mqtt\_printf(MQTT\_INFO, “Return code from MQTT connect is %d\n”, rc);  
> vTaskDelay(1000 / portTICK\_PERIOD\_MS);  
> }  
> mqtt\_printf(MQTT\_INFO, “MQTT Connected”);

1. For the server certificate.  
Below is for [test.mosquitto.org](http://test.mosquitto.org)  
[https://test.mosquitto.org/ssl/mosquitto.org.crt](https://test.mosquitto.org/ssl/mosquitto.org.crt)

And for HiveMQ Cloud Cluster I picked up follow below FAQs.

```auto
My device requires a server CA file to connect via TLS. How can I generate this for my HiveMQ Cloud instance?

```

[https://community.hivemq.com/t/frequently-asked-questions/514](https://community.hivemq.com/t/frequently-asked-questions/514)

Then, server certificate embed to above `char* rootCABuff` code.

Regards,

---

_[View the full topic](https://community.hivemq.com/t/client-is-not-authorized-to-connect/856)._
