我正在学习使用Watson Speech JS SDK。我特别喜欢Transcribe from Microphone, with Alternatives。我正在使用Firebase Cloud Function生成令牌。我正在使用AngularJS,而不是JQuery。我遇到的第一个问题是
var stream = WatsonSpeech.SpeechToText.recognizeMicrophone(Object.assign(token, {
objectMode: true,
format: false,
wordConfidence: true
}));
我收到此错误消息:
WatsonSpeechToText: missing required parameter: opts.token
(使用$scope.token
或token
没有区别。]
在documentation中查找此错误:
module.exports = function recognizeMicrophone(options) {
if (!options || !options.token) {
throw new Error('WatsonSpeechToText: missing required parameter: opts.token');
}
确定,它正在寻找options
对象。我使用以下代码修复了错误:
const options = {
token: $scope.token,
objectMode: true,
format: false,
wordConfidence: true
};
console.log(options);
var stream = WatsonSpeech.SpeechToText.recognizeMicrophone(options);
现在我收到此错误:
WebSocket connection to 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?model=en-US_BroadbandModel&watson-token=[object%20Object]' failed: HTTP Authentication failed; no valid credentials available
options
对象记录此:
token:
access_token: "eyJraWQiOiIyMDIwMDIyNTE4MjgiLCJhbGciOiJSUzI1NiJ9.eyJpYW1faWQiOiJp0tU2..."
expiration: 1585332575
expires_in: 3600
refresh_token: "OKC8z8ebLMzZcrAt6YgInnJJn0UIx1P3NTeDvdEC3kJqIQ7Yn9J9iu6-DF..."
scope: "ibm openid"
token_type: "Bearer"
objectMode: true
format: false
wordConfidence: true
smart_formatting: false
令牌是一个JSON对象,其中包括access_token
。这是SDK想要的吗? RecognizeStream documentation不会说是要JSON令牌还是只需要裸露的access_token
。
将000
添加到expiration
字段显示该令牌还剩53分钟。
我正在使用特定于我的语音转文本服务的API密钥。
还有其他建议吗?
[当IBM将具有用户名/密码身份验证的Cloud Foundry服务移至具有IAM身份验证和api密钥的服务时,该属性从token
更改为accessToken
。如果使用api键,options
对象将如下所示:
const options = {
accessToken: $scope.token,
objectMode: true,
format: false,
wordConfidence: true
};
如果省略令牌属性,则会收到此错误消息:
WatsonSpeechToText: missing required parameter: opts.token (CF) or opts.accessToken (RC)
这意味着,如果您要从Cloud Foundry(CF)获取令牌,则该属性必须为opts.token
(或options.token
);但是,如果您从IAM身份验证和一个无名叫作RC
的api密钥获取令牌,则该属性必须为opts.accessToken
(或options.accessToken
)。
令人困惑的是,demo source code暗示access_token
是属性名称:
var stream = WatsonSpeech.SpeechToText.recognizeMicrophone(Object.assign(token, {
objectMode: true,
format: false,
wordConfidence: true
}));
Object.assign
正在使用来自IBM的target
令牌对象,然后在source
对象中添加或替换属性和值。由于该属性在IAM令牌中为access_token
,因此您会认为,由于演示正在运行,因此access_token
是该属性。但是显然,该演示是在Cloud Foundry令牌上运行的,该令牌可以使用token
或access_token
作为属性名称。
如果收到此错误消息:
WatsonSpeechToText: missing required parameter: opts.token (CF) or opts.accessToken (RC)
然后您既不使用token
也不使用accessToken
作为属性名称。
如果收到此错误消息:
WebSocket connection to 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?model=en-US_BroadbandModel&watson-token=[object%20Object]' failed: HTTP Authentication failed; no valid credentials available
然后您将token
与通过IAM api密钥生成的令牌一起使用。否则您的令牌已过期。您可以通过将以下代码放入您的应用中来告诉您令牌还剩多少分钟,从而轻松进行检查:]
// shows time to token expiration var expiry = new Date($scope.token.expiration * 1000); var now = Date.now(); var duration = -(now - expiry); function msToTime(duration) { var milliseconds = parseInt((duration % 1000) / 100), seconds = Math.floor((duration / 1000) % 60), minutes = Math.floor((duration / (1000 * 60)) % 60), hours = Math.floor((duration / (1000 * 60 * 60)) % 24); hours = (hours < 10) ? "0" + hours : hours; minutes = (minutes < 10) ? "0" + minutes : minutes; seconds = (seconds < 10) ? "0" + seconds : seconds; return "Token expires in " + minutes + ":" + seconds + " minutes:seconds"; } console.log(msToTime(duration))
您可以在CLI中测试您的令牌是否有效。首先获得一个新令牌:
curl -k -X POST \ --header "Content-Type: application/x-www-form-urlencoded" \ --header "Accept: application/json" \ --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" \ --data-urlencode "apikey=s00pers3cret" \ "https://iam.cloud.ibm.com/identity/token"
然后请求语言模型:
curl -X GET "https://stream.watsonplatform.net/speech-to-text/api/v1/models?access_token=eyJraWQiO...."
我的代码中还有另一个问题。我正在更新IBM Speech-to-Text SDK,但是我的代码链接到三年前在bower中安装的旧SDK。我没有意识到当0.38.0是最新版本时我正在使用0.33.1。将此添加到您的代码以解决此问题:
console.log (WatsonSpeech.version);
使用旧的SDK,我收到的旧错误消息甚至都没有提及新的属性名称:
WatsonSpeechToText: missing required parameter: opts.token