簡體   English   中英

使用在服務器端獲取的iBM Bluemix音調分析器令牌時,在客戶端發生錯誤

[英]Error on client side when using iBM Bluemix tone analyzer token fetched on server side

我已經在服務器端獲得了一個令牌並將其存儲在cookie中,但是我似乎無法弄清楚為什么我用該令牌查詢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:jsonpdataType:jsonp收到無訪問控制源錯誤。 當我不使用contentType:application/json或使用contentType:application/javascript ,當查詢api要求輸入用戶名和密碼時,會出現一個登錄對話框。 但是,現在我已經有了令牌,就不必傳遞用戶名和密碼了。 當我用dataType和contentType這樣運行時,收到一個400錯誤的錯誤請求。

有人知道我在做什么錯嗎? 文檔說我可以在客戶端使用令牌。但是我必須在服務器端獲取令牌。

更新:

按照建議,我不會通過單獨的php文件的jquery ajax調用來訪問服務器端代碼。 我能夠獲得令牌,但是當我將其傳遞給音頻分析器的api調用時,出現了400錯誤。 不管我是否解碼令牌的令牌。

這是我的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');
});

還有獲取令牌的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用作正文參數時,應將其作為標頭,而將version作為查詢參數。 同樣,在您的JQuery rest調用的數據字段中,您正在對已經被字符串化的對象進行字符串化,並且在標頭中對不需要解碼的令牌響應進行解碼。

您可以在此處找到有關如何創建對Watson音調分析器服務的呼叫的更多信息。

編輯:這是使用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();
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM