【问题标题】:Sentiment Analysis using Google Cloud API in Python giving error在 Python 中使用 Google Cloud API 进行情绪分析给出错误
【发布时间】:2020-09-21 09:16:56
【问题描述】:

我正在尝试使用 google NLP 云 API 执行情绪分析。

下面是我的代码

import requests

url = "https://language.googleapis.com/v1/documents:analyzeSentiment"
myobj = {
    "key": "XYZ",
    "document":{
        "type":"PLAIN_TEXT",
        "language": "EN",
        "content":"'Lawrence of Arabia' is a highly rated film biography about British Lieutenant T. E. Lawrence. Peter O'Toole plays Lawrence in the film."
      },
    "encodingType":"UTF8",
    "Content-Type": "application/json"
}

x = requests.post(url, data = myobj)

print(x.text)

但它给了我错误

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"Content-Type\": Cannot bind query parameter. Field 'Content-Type' could not be found in request message.\nInvalid JSON payload received. Unknown name \"document\": Cannot bind query parameter. 'document' is a message type. Parameters can only be bound to primitive types.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"Content-Type\": Cannot bind query parameter. Field 'Content-Type' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"document\": Cannot bind query parameter. 'document' is a message type. Parameters can only be bound to primitive types."
          }
        ]
      }
    ]
  }
}

有人知道为什么会这样吗?将不胜感激任何帮助。谢谢。

【问题讨论】:

    标签: python google-cloud-platform


    【解决方案1】:

    使用json 参数代替data

    x = requests.post(url, json=myobj)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多