{"error": "unauthorized", "message": "Token expired"}
I am implementing a persistent WebSocket connection to the Genesys Cloud Notification API (wss://api.us.genesyscloud.com/v2/analytics/events) using Node.js. My application uses an OAuth client_credentials token.
The documentation states:
“The server will send a close frame with code 4001 when the token expires. The client must obtain a new token and reconnect.”
I implemented a reconnection handler in my Node.js client:
ws.on('close', (code, reason) => {
if (code === 4001) {
console.log('Token expired, refreshing...');
getToken().then(token => {
ws = new WebSocket(url, { headers: { 'Authorization': `Bearer ${token}` } });
setupListeners(ws);
});
}
});
However, the reconnection fails immediately with a 401 Unauthorized handshake error, even though getToken() returns a valid token (verified via Postman). The initial connection works fine. Why does the reconnection fail with 401 when using the same token generation logic? Is there a specific header requirement for the WebSocket upgrade request that differs from the initial connection?