0

Ist es möglich, Google Natural Language Cloud API mit nur einem einfachen API-Schlüssel zu verwenden? Die authentication documents scheinen darauf hinzudeuten, dass es ist, indem man vorschlägt, es für cURL-Tests zu verwenden, aber wenn ich mit dem untenstehenden Code versuche, erhalte ich eine 404-Nachricht über die URL.Einfache Authentifizierung mit Google Natural Language API mit nur Client-Javascript

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

Antwort

0

es funktioniert, ich habe gerade die falsche Methode angegeben:

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