0

我正在尝试使用 node.js 的 websocket 模块与 IBM Watson Speech to 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 凭据。

4

2 回答 2

1

为了使用 WebSockets,您首先需要token调用authorizationapi。然后你将把它添加tokenurl.

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-cloudnpm 模块。看看这个片段,它向您展示了如何使用 Speech to Text 服务进行实时转录。

于 2015-08-08T02:11:24.343 回答
0

Watson 的 Speech-to-Text 服务不支持 Web Sockets。可以在这里找到详细的解释

您将需要使用不同的协议。可以在此处找到通过 Node.js 连接到 Watson API 的指南。

希望这可以帮助!

于 2015-08-07T20:02:58.887 回答