【问题标题】:Authorization issue for Watson Text to Speech from Java scriptJavascript 中的 Watson Text to Speech 的授权问题
【发布时间】:2018-03-06 02:51:08
【问题描述】:

我正在尝试在 javascript 代码中使用 Watson 文本到语音服务。 但是,我一直在努力让它发挥作用。

如果我使用以下代码:

 $.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: 请求头域 Authorization is not allowed by 预检响应中的 Access-Control-Allow-Headers。

请注意,我可以很容易地从 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.

【问题讨论】:

标签: cors basic-authentication restful-authentication ibm-watson watson


【解决方案1】:

看起来 IBM Watson Text To Speech 确实支持部分 CORS(在您的情况下需要)。请检查该答案: Can't access IBM Watson API locally due to CORS on a Rails/AJAX App

此外,您会在此处找到一个明智的建议,通知您不要在 JavaScript 代码中添加您的 Watson 凭据,而是使用令牌: https://console.bluemix.net/docs/services/watson/getting-started-tokens.html#tokens-for-authentication

当您在客户端工作时,也许尝试 Watson 的 NPM 模块或库(带有示例)将是一个不错的选择:

https://www.npmjs.com/package/watson-speech

https://watson-speech.mybluemix.net/text-to-speech.html

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多