简体   繁体   中英

IBM Bluemix and websocket 400 error

I'm trying to use the websocket module for node.js to interface with the IBM Watson Speech to text api. When I try to connect I get a 400 error and I'm not really sure why... I've never used websockets before. Here is my code creating the socket and trying to connect

var WebSocketClient = require('websocket').client,
    client = new WebSocketClient(),
    token = 'myToken==',
    wsri = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=' + token;

//some event handlers for on connect and on connectFailed

client.connect(wsri, null, null, null, null);

Here is the response I get

Connect Error: Error: Server responded with a non-101 status: 400
Response Headers Follow:
content-type: text/html
x-dp-watson-tran-id: csf_platform_prod_dp01-735083801
set-cookie: Watson-DPAT=this_is_a_cookie; path=/speech-to-text/api; secure; HttpOnly
www-authenticate: Basic realm="IBM Watson Gateway Log-in"
x-backside-transport: FAIL FAIL
connection: close

any ideas how to fix this??

EDIT-UPDATE: Germans answer below is correct. I wasn't calling the authorization endpoint to get a token and was trying to use my bluemix credentials.

In order to use WebSockets you first need to get a token calling the authorization api. Then you will add that token into the url .

The Websocket url is:

wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=TOKEN

Where TOKEN can be created by doing (example in curl):

 curl -u USERNAME:PASSWORD  "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"

USERNAME and PASSWORD are your service credentials.

That's basically a GET request to

https://stream.watsonplatform.net/authorization/api/v1/token

with a url query parameter that is the service you want to get the token for. In this case:

https://stream.watsonplatform.net/speech-to-text/api

If you are using nodejs, I would suggest you to use the watson-developer-cloud npm module. Take a look at this snippet which shows you how to do real time transcription using the Speech to Text service.

Watson's Speech-to-Text Service does not support Web Sockets. A detailed explanation can be found here .

You are going to need to use a different protocol. A guide to connecting to Watson's API via Node.js can be found here .

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM