【问题标题】:Simple authentication with Google Natural Language API using only client javascript仅使用客户端 javascript 使用 Google Natural Language API 进行简单身份验证
【发布时间】:2017-03-13 11:38:29
【问题描述】:

是否可以仅使用一个简单的 API 密钥来使用 Google 的 Natural Language Cloud API? authentication documents 似乎表明它是通过建议将其用于 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>

【问题讨论】:

    标签: javascript google-cloud-platform google-authentication google-language-api


    【解决方案1】:

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

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 2012-01-10
      • 2016-05-14
      • 1970-01-01
      • 2022-10-30
      • 2019-01-07
      相关资源
      最近更新 更多