[英]Google's Cloud Natural Language API throwing “Request contains an invalid argument” error (400)
Trying to use the documents.analyzeSentiment method in the NLP API but it's throwing an error indicating that the argument is invalid.尝试在 NLP API 中使用documents.analyzeSentiment方法,但它抛出一个错误,表明参数无效。
The error being presented is this -出现的错误是这样的 -
{error={code=400, message=Request contains an invalid argument., status=INVALID_ARGUMENT}}
{error={code=400, message=Request 包含无效参数。, status=INVALID_ARGUMENT}}
This is what I'm trying out on Apps Script -这就是我在 Apps Script 上尝试的 -
function analyzeText() {
var apiKey = 'MyAPIKeyViaCloudConsole'
var text = "I love R&B music. Marvin Gaye is the best. 'What's Going On' is one of my favorite songs. It was so sad when Marvin Gaye died.";
var requestUrl = 'https://language.googleapis.com/v1/documents:analyzeSentiment?key=' + apiKey;
var data = {
"document": {
"language": "en",
"content": text
},
"encodingType": "UTF8"
};
var options = {
method : "POST",
contentType: "application/json",
payload : JSON.stringify(data),
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(requestUrl, options);
var data = JSON.parse(response);
Logger.log(data);
}
Basically, I intend to some sort of sentiment analysis on a whole bunch of things (client remarks, some tweets etc.) those of what I'll deal with in batches once I've gotten the code to work.基本上,我打算对一大堆事情(客户评论,一些推文等)进行某种情绪分析,一旦我让代码工作,我将分批处理这些事情。
What am I doing wrong??我究竟做错了什么?? :(
:(
Looks like you'd be required to provide the "type" in your data param + I also updated the "language" from plan en to en-us .看起来您需要在数据参数中提供“类型” +我还将“语言”从 plan en更新为en-us 。
Try this -尝试这个 -
function analyzeText() {
var apiKey = 'YourAPIKey'
var text = "Random Text as required";
var requestUrl = 'https://language.googleapis.com/v1/documents:analyzeSentiment?key=' + apiKey;
var data = {
"document": {
"language": "en-us",
"type": "PLAIN_TEXT",
"content": text
},
"encodingType": "UTF8"
};
var options = {
method : "POST",
contentType: "application/json",
payload : JSON.stringify(data),
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(requestUrl, options);
var data = JSON.parse(response);
Logger.log(data);
}
Obviously, based on your use case, you may want to iterate a batch of text via a for
loop.显然,根据您的用例,您可能希望通过
for
循环迭代一批文本。
Also, please ensure that your Google Cloud Console Project has a billing account associated with it to run the Natural Language API.此外,请确保您的 Google Cloud Console 项目具有与之关联的结算帐户,以运行自然语言 API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.