是否可以使用一个简单的API键来使用谷歌的自然语言云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>