我正在学习使用Watson Speech JS SDK。我特别喜欢Transcribe from Microphone, with Alternatives。我正在使用Firebase云函数生成我的令牌。我使用的是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 availableoptions对象记录如下:
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密钥。
还有其他建议吗?
发布于 2020-04-01 02:06:15
SDK的0.37.0版本引入了突破性的变化:
All options parameters for all methods are coverted to be lowerCamelCase
For example: access_token is now accessToken and content-type is now contentType这与IBM从使用用户名/密码身份验证的Cloud Foundry服务转移到使用IAM身份验证和api密钥的服务相关。SDK documentation说:
NOTE: The token parameter only works for CF instances of services. For RC services using IAM for authentication, the accessToken parameter must be used.如果使用API键,则options对象如下所示:
const options = {
accessToken: $scope.token,
objectMode: true,
format: false,
wordConfidence: true
};如果省略了token属性,则会收到以下错误消息:
WatsonSpeechToText: missing required parameter: opts.token (CF) or opts.accessToken (RC)这意味着,如果您从Cloud Foundry (CF)获取令牌,则属性必须为opts.token (或options.token);但如果您从IAM auth和api-key获取令牌,则属性必须为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令牌对象,然后添加或替换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-key生成的令牌一起使用。或者你的令牌过期了。你可以通过在你的应用程序中添加以下代码来轻松检查,告诉你你的令牌还剩多少分钟:
// 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.33.1,而0.38.0是最新版本。将以下代码添加到您的代码中以捕获此问题:
console.log (WatsonSpeech.version);使用旧的SDK,我得到了一个旧的错误消息,甚至没有提到新的属性名称:
WatsonSpeechToText: missing required parameter: opts.token我的blog post中有更多关于IBM Cloud语音到文本的信息。
https://stackoverflow.com/questions/60891348
复制相似问题