简体   繁体   English

Google 的 Cloud Natural Language API 抛出“Request contains an invalid argument”错误(400)

[英]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.

相关问题 标准幻灯片操作后的错误响应 400“请求包含无效参数” - Error response 400 "Request contains an invalid argument" after the standard Slides manipulation 如何使 Cloud Natural Language 的分类文本 API 与 AppScript 一起工作 - How to make Cloud Natural Language classifyText API work with AppScript 在 Apps 脚本和 Google 自然语言 API 上循环 function - Loop function on Apps Scripts & Google Natural Language API 使用 Google Enterprise License Manager API 插入 G Suite licenseAssignment 时出现错误 400“无效的 JSON 有效负载” - Error 400 “Invalid JSON payload” when inserting G Suite licenseAssignment with Google Enterprise License Manager API 在Google Apps脚本中使用Wunderlist API时出现“无效请求”错误 - 'invalid request' error while utilizing wunderlist api in Google Apps Script Google Photos API:请求更新 mediaItem 描述时出现 400 错误 - Google Photos API: 400 error when making request to update mediaItem description 谷歌工作表宏错误“异常:无效参数” - Google Sheet Macro Error "Exception: Invalid Argument" docs.google.com 的请求失败,错误代码为 400 - request failed with error code 400 for docs.google.com Kraken API 私有请求身份验证 {"error":["EAPI:Invalid key"]} - Google 脚本 - Kraken API private request authentication {"error":["EAPI:Invalid key"]} - Google Script Google Picker API Invalid origin value 错误 - Google Picker API Invalid origin value error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM