[英]How do you fetch JSON from Google's Cloud Natural Language API based on existing text using client-side JS?
[英]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.