- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经在服务器端获得了一个 token 并将其存储在 cookie 中,但我似乎无法弄清楚为什么当我使用该 token 查询 api 时会收到错误。
这是我发送的 jQuery ajax 请求:
$.ajax({
url:'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone',
data:{
'X-Watson-Authorization-Token':readCookie('token'),
'text':input,
'version':'v3',
'version_date':'2016-05-19'
},
dataType:'jsonp',
contentType:'application/json',
method:'GET',
success:function(tone){
console.log(tone);
}
});
如果我不使用dataType:jsonp
,我会收到无访问控制源错误。当我不使用 contentType:application/json
或使用 contentType:application/javascript
时,在查询 api 时会出现一个登录对话框,要求输入用户名和密码。但既然我已经有了 token ,我就不必传递用户名和密码了。当我以这种方式运行它时,同时使用 dataType 和 contentType,我收到 400 错误错误请求。
有人知道我做错了什么吗?文档说我可以在客户端使用 token 。但我必须在服务器端获取它。
更新:
根据建议,我没有通过单独 php 文件的 jquery ajax 调用来访问服务器端代码。我能够获取 token ,但是当我将其传递给音调分析器的 api 调用时,我收到 400 错误。不管我是否解码 URI token 。
这是我的 jquery:
$.when($.ajax({
url:'watsonToken.php',
type:'GET',
})).done(function(token){
console.log('watsonToken, lasts 1 hour: ', decodeURI(token));
$.ajax({
url:'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone',
type:'POST',
data:JSON.stringify({
'body':input,
'version':'2016-05-19'
}),
contentType:'application/json',
headers:{
'X-Watson-Authorization-Token':decodeURI(token)
},
success:function(tone){
console.log(tone);
},
error: function(error){
console.error('Error: Couldn\'t use token: ', error);
}
});
}).fail(function(){
console.error('Error: Couldn\'t fetch watson token');
});
以及获取 token 的 watsonToken.php 文件:
<?php
$accessWatsonToken = curl_init();
$params=http_build_query(array('url' => 'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone'));
curl_setopt($accessWatsonToken, CURLOPT_URL, "https://gateway.watsonplatform.net/authorization/api/v1/token?$params");
curl_setopt($accessWatsonToken, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($accessWatsonToken, CURLOPT_USERPWD, 'username':password');
print curl_exec($accessWatsonToken);
curl_close($accessWatsonToken);
?>
最佳答案
我认为您正在尝试将 X-Watson-Authorization-Token
作为正文参数,而它应该是 header ,而 version
应该是查询参数。此外,在 JQuery 休息调用的数据字段中,您正在对已经字符串化的对象进行字符串化,并且在 header 中您正在解码不需要解码的 token 响应
您可以找到有关如何创建对 Watson 音调分析器服务的调用的更多信息 here
编辑:这是一个使用 PHP 的完整示例。
index.php
<!doctype html>
<html lang="en">
<head>
<title>Watson Tone Analyzer Example</title>
<meta charset="utf-8"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<h2>This is an example of client side call to Watson Tone analyzer service using an authorization token.</h2>
<div id="myoutput"></div>
</body>
</html>
<script>
analyze();
function analyze(){
$.ajax({
url:'/get-token.php',
type:'GET',
success:function(token){
callToneAnalyzer(token);
},
error: function(err) {
console.error(err);
}
});
}
function callToneAnalyzer(token) {
$.ajax({
url:'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2016-05-19',
type:'POST',
data: JSON.stringify({text: "this is my sample text"}),
contentType: 'application/json',
headers: {
'X-Watson-Authorization-Token': token
},
success:function(tone){
$("#myoutput").text(JSON.stringify(tone));
},
error: function(err) {
$("#myoutput").text(JSON.stringify(err));
}
});
}
</script>
get-token.php
<?php
// Send a http request using curl
function getToken(){
$username='YOUR-TONE-ANALYZER-USERNAME';
$password='YOUR-TONE-ANALYZER-PASSWORD';
$URL='https://gateway.watsonplatform.net/authorization/api/v1/token?url=https://gateway.watsonplatform.net/tone-analyzer/api';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
curl_close ($ch);
return $result;
}
echo getToken();
?>
关于javascript - 使用在服务器端获取的 IBM Bluemix 音调分析器 token 时客户端出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40262937/
我环顾了 Google 和 SO,但找不到这个...... 有没有一种方法可以在 Mac 上仅使用 Python 标准库来生成简单的音调(即蜂鸣声、方波、锯齿声)? 基本上,我正在尝试在文本角色扮演游
我搜索了很多术语和量刑,但找不到任何相关信息。所以无论如何:我怎样才能为警报(默认应用程序)获取默认或当前设置的音乐/音调/声音? 假设我每天早上醒来时都会播放闹钟歌曲:Song#1 使用默认闹钟应用
我正在读取来自麦克风的原始波流。 (这部分工作,因为我可以将它发送到扬声器并得到很好的回声。) 为简单起见,假设我想检测波形数据中的 DTMF 音调。实际上我想检测任何频率,而不仅仅是 DTMF 中的
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是偏离
Android 的新手。我有一个应用程序正在运行,我正在生成 DTMF 信号并解释微 Controller 生成的内容。我成功地生成了音调,但是音调是通过扬声器播放的,我想通过一个拼接的耳机插孔播放音
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我录制了几秒钟的音频,其中包含以相同频率生成的两个相似的音调。他们相隔几秒钟。我想做的是根据该音频文件的采样间隔,检测出第一声的结束和第二声的开始。假设以48KHz音频的16位带符号PCM和一个字节数
我有 wav 文件,使用 naudio 库我已经能够从 wav 文件中获取原始数据。 有谁知道如何循环检测 DTMF 音调的 block 中的数据? 最佳答案 NuGet 包 DtmfDetectio
我有一个使用 OpenEars API 读取文本的 iOS 应用程序。我使用的是最新版本 (1.2.5)。我无法弄清楚如何在阅读单词时改变音调(“即时”)。我创建了一个 slider 来控制音高。更改
如何在 iPhone 中以编程方式检测 dtmf 音调?。我想检测接收到的音频信号(麦克风输入)是否应该是 dtmf 音调? 请帮助我?我真的为此苦苦挣扎了两天。提前致谢...... 最佳答案 由于
我想在 android 中执行以下操作。可能吗? 1) 调用+xx xxxxxxx 2) 等待第一声结束 3) 插入通行证:xxxx 4)等待短高音结束 5) 按 1 直到听到特定的声音。 这里的音调
当对方在通话中按 1,2,3.....9 时,是否可以在 android 中获取 DTMF 音调?如果是,怎么办? 最佳答案 查看此 http://code.google.com/p/dtmf-dec
我目前正在研究汽车安全系统.... 我需要通过输入到外部数字电路的电话耳机产生 DTMF 音调。是否有任何 API 可以执行此操作?任何人都可以帮助.... 提前致谢... 最佳答案 我得到了答案..
我想在 php 中生成一个正弦波。但是构建我的 wav 我需要以字节为单位给出值。我不知道该怎么做: 这是我的代码: $freqOfTone = 440; $sampleRate = 44100; $
我正在尝试在 linphone 的 SIP 调用期间发送一系列 DTMF 音调,为 iPhone 编译,以便在我设置的本地交换机上进行一些调用管理。我从代码中看到各个数字发送 DTMF(线路上没有音频
我正在尝试从电话线上检测来电显示信息。收到的来电显示信息采用 DTMF 音调(而非 FSK)。我怎样才能检测到这个 DTMF 音调?电话和传真调制解调器可以吗?电话和传真调制解调器能够检测 FSK 信
这就是我所拥有的。它生成 5 秒 Au file具有 440 Hz 正弦波,灵感来自 this question . -- file: tone.hs import qualified Data.By
我有一个 double 组,这是对数组应用 FFT 的结果,其中包含我添加了 1000Hz 音调的 Wav 音频文件的音频数据。 我通过“数值食谱”中定义的 DREALFT 获得了这个数组。(我必须使
Tone.BufferSource:缓冲区未设置或未加载。此错误发生在 try/catch block 中。它只会在我不断或有时随机触发更新功能时发生。发生此错误时,我的音频会暂时关闭。 我的代码背后
我正在 iphone 上开发一个应用程序,我需要一种方法来检测通过麦克风传入的声音的音调。 (即 A#、G、C♭ 等)我以为我会使用 AVAudio,但我真的不知道,而且我在文档中找不到任何内容。 有
我是一名优秀的程序员,十分优秀!