Hello Community, I am currently working on a project where I am using a pre-signed URL to connect to an MQTT server. However, I am consistently receiving a 403 error when attempting to establish the connection. Interestingly, when I use the same pre-signed URL with the AWS IoT library, the connection is successful and everything works as expected. This leads me to believe that the issue is not with the URL itself, but perhaps with how I am using it in my project. Here is the code snippet where I am using the pre-signed URL:
private const val completeUrl = "wss://XXXX-ats.iot.us-east-1.amazonaws.com:443/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIAWMVZ7DAMWRZXXXXFus-east-1%2Fiotdevicegateway%2Faws4_request&X-Amz-Date=20240725T171507Z&X-Amz-SignedHeaders=host&X-Amz-Signature=SIGNATURE&X-Amz-Security-Token=TOKEN"
fun connect(completeUrl: String) {
val uri = URI(completeUrl)
val client = MqttClient.builder()
.identifier("eb368f23-5278-4bdf-9fda-2cdb023be1ca")
.serverHost(uri.host)
.serverPort(uri.port)
.sslWithDefaultConfig()
.webSocketConfig(
MqttWebSocketConfig.builder()
.serverPath(uri.path)
.queryString(uri.query)
.subprotocol("mqtt")
.build()
).useMqttVersion5()
.buildAsync()
client.connectWith()
.send()
.whenComplete { _, throwable ->
if (throwable != null) {
throwable.printStackTrace()
} else {
println("Connected")
}
}
}
This is the error that I got it:
com.hivemq.client.mqtt.exceptions.ConnectionFailedException: io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException: Invalid handshake response getStatus: 403 Forbidden
Caused by: io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException: Invalid handshake response getStatus: 403 Forbidden
at io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker13.verify(WebSocketClientHandshaker13.java:312)
at io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker.finishHandshake(WebSocketClientHandshaker.java:364)
at com.hivemq.client.internal.mqtt.handler.websocket.MqttWebsocketHandshakeHandler.finishHandshake(MqttWebsocketHandshakeHandler.java:103)
at com.hivemq.client.internal.mqtt.handler.websocket.MqttWebsocketHandshakeHandler.channelRead(MqttWebsocketHandshakeHandler.java:94)