Google翻译API身份验证[邮递员]

时间:2020-10-22 08:29:56

标签: api https google-api request postman

我在使用Google Translate API时遇到了问题(之前有很多问题...)。我需要通过Postman运行测试查询以获取简单的字符串翻译。没什么花哨的。

我正在尝试根据文档构建请求,但目前看来我失败了。

使用API​​密钥时,Google表示我应该直接将key=MY_KEY直接添加到请求字符串中。

这与以下问题类似:Why Google Translate API doesn't accept API-Key in JSON request body?,但是没有人对此提供良好的答案(我也遇到了另一个错误)

我当前的示例如下:

POST
https://translation.googleapis.com/language/translate/v2
?q=The Great Pyramid of Giza (also known as the Pyramid of Khufu or the Pyramid of Cheops) is the oldest and largest of the three pyramids in the Giza pyramid complex.
&source="en"
&target="es"
&format="text"
&key=MY_KEY_STRING

不幸的是,我得到400:无效值

{
"error": {
    "code": 400,
    "message": "Invalid Value",
    "errors": [
        {
            "message": "Invalid Value",
            "domain": "global",
            "reason": "invalid"
        }
    ]
}

也许有人知道问题出在哪里?干杯!

1 个答案:

答案 0 :(得分:1)

您需要在帖子正文中发送请求,这样才能告诉邮递员其内容类型为json

POST https://translation.googleapis.com/language/translate/v2?key=YourKey

{
  "key": "xxxxxxxxxx",
  "q": "The Great Pyramid of Giza (also known as the Pyramid of Khufu or the Pyramid of Cheops) is the oldest and largest of the three pyramids in the Giza pyramid complex.",
  "source": "en",
  "target": "es",
  "format": "text"
}