繁体   English   中英

仅使用客户端javascript与Google自然语言API进行简单身份验证

[英]Simple authentication with Google Natural Language API using only client javascript

是否可以仅使用一个简单的API密钥即可使用Google的自然语言云API? 身份验证文档似乎建议通过将其用于cURL测试来表明它,但是当我尝试以下代码时,URL上显示404消息。

<html>
  <head>
    <script src="https://apis.google.com/js/api.js"></script>
    <script>
      function start() {
        gapi.client.init({
          'apiKey': 'XXXXXXX'
        }).then(function() {
          return gapi.client.request({
            path: '/v1beta1/documents:analyzeSentiment',
            method: 'POST',
            body: {'document': {
                      'type': 'PLAIN_TEXT',
                      'content': 'ANALZE THIS, IS IT BAD?'
                   } 
            }  
          });
        }).then(function(resp) {
          console.log(resp.result);
        }, function(reason) {
          console.log('Error: ' + reason.result.error.message);
        });
      };
      gapi.load('client', start);
    </script>
  </head>
  <body>
    <div id="results"></div>
  </body>
</html>

它确实有效,只是指定了错误的方法:

<html>
  <head>
    <script src="https://apis.google.com/js/api.js"></script>
    <script>
      function start() {
        gapi.client.init({
          'apiKey': 'XXXX',
          'discoveryDocs': ['https://language.googleapis.com/$discovery/rest?version=v1beta1']
        }).then(function() {
          return gapi.client.language.documents.analyzeSentiment({
            // not sure how to put in a JSON object in here correctly
            'document': {
                      'type': 'PLAIN_TEXT',
                      'content': 'ANALZE THIS YOU MARVELLOUS PERSON'
                   }
          });
        }).then(function(resp) {
          console.log(resp.result);
        }, function(reason) {
          console.log('Error: ' + reason.result.error.message);
        });
      };
      gapi.load('client', start);
    </script>
  </head>
  <body>
    <div id="results"></div>
  </body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM