- Environment: Genesys Cloud API v2
- Tool: PowerShell 7.4
- Goal: Custom chat UI via WebSocket Guest API
Can anyone clarify why my WebSocket handshake fails with a 401 error when trying to connect to the Guest API? I am building a custom chat interface in PowerShell to avoid the default widget overhead. I successfully fetch the user token using Invoke-RestMethod against /api/v2/oauth/token. The token returns a valid access_token string.
However, when I attempt to open the WebSocket connection to wss://api.mypurecloud.com/api/v2/guests/conversations/channels/websocket, the connection is immediately rejected. I am passing the token in the Authorization header as Bearer <token>. Here is the minimal repro code:
$token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..."
$headers = @{ Authorization = "Bearer $token" }
try {
$ws = New-Object System.Net.WebSockets.ClientWebSocket
$task = $ws.ConnectAsync(new Uri("wss://api.mypurecloud.com/api/v2/guests/conversations/channels/websocket"), $null)
$task.Wait()
Write-Host "Connected" -ForegroundColor Green
} catch {
Write-Host "Failed: $_" -ForegroundColor Red
}
The error message is The remote server returned an error: (401) Unauthorized. I confirmed the token works for standard REST calls like /api/v2/users/me. Is the WebSocket endpoint requiring a different scope or header format? I am in Australia/Sydney timezone if latency matters, but this fails instantly.