我正在为IBM Watson Speech-to-text做教程。在" Using the WebSocket interface",小节"打开连接并传递凭据",我复制了以下代码:
var token = watsonToken;
console.log(token); // token looks good
var wsURI = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=' +
token + '&model=es-ES_BroadbandModel';
var websocket = new WebSocket(wsURI);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
我正在使用Angular,因此我为令牌创建了一个值:
app.value('watsonToken', 'Ln%2FV...');
我收到一条错误消息:
WebSocket connection to 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-toke...&model=es-ES_BroadbandModel' failed: HTTP Authentication failed; no valid credentials available
我尝试对令牌进行硬编码:
var wsURI = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=Ln%2FV2...&model=es-ES_BroadbandModel';
相同的错误消息。
关于tokens的IBM文档说,过期或无效的令牌将返回401错误,我没有得到,所以我假设我的令牌既没有过期也没有效果。有什么建议吗?
答案 0 :(得分:0)
我认为您可以从IBM Developers here看到官方示例。
错误是因为在您发送请求识别之前身份验证无法正常工作,请尝试按照此存储库中的相同步骤操作,例如:
const QUERY_PARAMS_ALLOWED = ['model', 'X-Watson-Learning-Opt-Out', 'watson-token', 'customization_id'];
/**
* pipe()-able Node.js Readable/Writeable stream - accepts binary audio and emits text in it's `data` events.
* Also emits `results` events with interim results and other data.
* Uses WebSockets under the hood. For audio with no recognizable speech, no `data` events are emitted.
* @param {Object} options
* @constructor
*/
function RecognizeStream(options) {
Duplex.call(this, options);
this.options = options;
this.listening = false;
this.initialized = false;
}
util.inherits(RecognizeStream, Duplex);
RecognizeStream.prototype.initialize = function() {
const options = this.options;
if (options.token && !options['watson-token']) {
options['watson-token'] = options.token;
}
if (options.content_type && !options['content-type']) {
options['content-type'] = options.content_type;
}
if (options['X-WDC-PL-OPT-OUT'] && !options['X-Watson-Learning-Opt-Out']) {
options['X-Watson-Learning-Opt-Out'] = options['X-WDC-PL-OPT-OUT'];
}
const queryParams = extend({ model: 'en-US_BroadbandModel' }, pick(options, QUERY_PARAMS_ALLOWED));
const queryString = Object.keys(queryParams)
.map(function(key) {
return key + '=' + (key === 'watson-token' ? queryParams[key] : encodeURIComponent(queryParams[key])); // our server chokes if the token is correctly url-encoded
})
.join('&');
const url = (options.url || 'wss://stream.watsonplatform.net/speech-to-text/api').replace(/^http/, 'ws') + '/v1/recognize?' + queryString;
const openingMessage = extend(
{
action: 'start',
'content-type': 'audio/wav',
continuous: true,
interim_results: true,
word_confidence: true,
timestamps: true,
max_alternatives: 3,
inactivity_timeout: 600
},
pick(options, OPENING_MESSAGE_PARAMS_ALLOWED)
);
此代码来自IBM Developers,对于我正在使用并完美运行的项目。
您可以在代码行#53中看到,将听取设置为true
,否则当您发送没有语音的音频时,它会最终超时并自动关闭inactivity_timeout
在其中,而不是在您根本不发送任何数据时。
有另一个例子,请参阅IBM Watson的this示例 - 使用Javascript for Speech to Text的Watson Developer Cloud。
答案 1 :(得分:0)
首先,如果您使用IBMid和密码,则不会获得令牌。您必须使用为项目提供的用户名和密码。该用户名是一串字母和带连字符的数字。
其次,documentation for tokens为您提供获取令牌的代码:
curl -X GET --user {username}:{password}
--output token
"https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/text-to-speech/api"
部分代码隐藏在网页上,特别是/text-to-speech/
部分。您需要将其更改为您要使用的Watson产品或服务,例如/speech-to-text/
。代币用于特定项目和特定服务。
第三,令牌在一小时后到期。
最后,我必须输入反斜杠才能让代码在我的终端中运行:
curl -X GET --user s0921i-s002d-dh9328d9-hd923:wy928ye98e \
--output token \
"https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"