授权问题沃森文本到语音转换从Java脚本
问题描述:
我想使用沃森文本到语音服务的JavaScript代码。 但是,我试图让它工作。授权问题沃森文本到语音转换从Java脚本
如果我使用下面的代码:
$.ajax({
url: 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize',
type: 'POST',
headers: {"Content-Type": "application/json", "Accept": "audio/*", "Authorization": "Basic SomethingSomethingSomething=="},
text: msgData.message[0].cInfo.text,
output: 'output.wav',
success: function() {
console.log("text to voice complete");
var audio = new Audio('output.wav');
audio.play();
}
});
我得到:
无法加载 https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize: 请求头字段授权不被 访问控制允许报头允许在飞行前响应。
请注意,我可以很容易地得到这样的要求是从Restlet工作。
不过,如果我使用:
$.ajax({
url: 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize',
type: 'POST',
user: {"something": "something"},
headers: {"Content-Type": "application/json", "Accept": "audio/*"},
data: {"text": msgData.message[0].cInfo.body},
output: 'output.wav',
success: function() {
console.log("text to voice complete");
var audio = new Audio('output.wav');
audio.play();
}
});
我得到:
stream.watsonplatform.net/text-to-speech/api/v1/synthesize:1 POST https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize 401 (Processed)
index.html:1 Failed to load https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://52.207.232.200' is therefore not allowed access. The response had HTTP status code 401.
答
它看起来像IBM沃森文本到语音不支持部分 CORS(需要你的情况)。请检查答案: Can't access IBM Watson API locally due to CORS on a Rails/AJAX App
而且,你会发现有一个明智的建议,通知您不要在你的JavaScript代码添加您的沃森凭据,而是使用令牌: https://console.bluemix.net/docs/services/watson/getting-started-tokens.html#tokens-for-authentication
正如你“重新在客户端的工作,也许尝试沃森的NPM模块或库(带有示例)将是一个不错的选择:
https://www.npmjs.com/package/watson-speech
https://watson-speech.mybluemix.net/text-to-speech.html
希望这有助于!
见https://*.com/questions/43105146/how-to-call-ibm-watson-services-from-javascript/43106268#43106268 – sideshowbarker