我正在尝试使用websocket模块将node.js与IBM Watson Speech连接到text api。当我尝试连接时,我得到400错误,我不确定为什么......我以前从未使用过websockets。这是我的代码创建套接字并尝试连接
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);
以下是我得到的回复
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
任何想法如何解决这个问题?
编辑更新:德国人在下面回答是正确的。我没有调用授权端点获取令牌,并且正在尝试使用我的bluemix凭据。
答案 0 :(得分:1)
要使用WebSockets,首先需要token
调用authorization
api。然后,您将token
添加到url
。
Websocket网址是:
wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=TOKEN
可以通过执行创建TOKEN
(curl中的示例):
curl -u USERNAME:PASSWORD "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"
USERNAME
和PASSWORD
是您的服务凭据。
这基本上是GET
https://stream.watsonplatform.net/authorization/api/v1/token
使用url
查询参数,该参数是您要获取令牌的服务。在这种情况下:
https://stream.watsonplatform.net/speech-to-text/api
如果您使用的是nodejs,我建议您使用watson-developer-cloud
npm模块。请查看this片段,其中显示了如何使用语音到文本服务进行实时转录。
答案 1 :(得分:0)
Watson的语音转文本服务不支持Web套接字。 A detailed explanation can be found here
您将需要使用其他协议。可以找到通过Node.js连接到Watson API的指南here。
希望这有帮助!