gpt4 book ai didi

javascript - 使用在服务器端获取的 IBM Bluemix 音调分析器 token 时客户端出现错误

转载 作者:行者123 更新时间:2023-12-03 05:48:00 25 4
gpt4 key购买 nike

我已经在服务器端获得了一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com