为什么Google Translate API不接受JSON请求正文中的API-Key?

rek*_*eki 6 json google-api google-translate

我是新手使用Google Can Platform.

我尝试了Translation API的教程.出于某种原因,我想使用API​​-Key进行身份验证.但是API不接受JSON请求中的密钥,尽管它在HTTP查询参数中接受相同的密钥.

这是Google Translation API的限制吗?还是我有任何错误?

以下是我试过的:

在API密钥作为查询参数传递时工作

$ curl -H 'Content-Type: application/json' 'https://translation.googleapis.com/language/translate/v2?key=xxxxxxxxxx' --data-binary @test.json
Run Code Online (Sandbox Code Playgroud)

我的test.json是:

{
  "q": "The quick brown fox jumped over the lazy dog.",
  "source": "en",
  "target": "es",
  "format": "text"
}
Run Code Online (Sandbox Code Playgroud)

结果是:

{
  "data": {
    "translations": [
      {
        "translatedText": "El rpido zorro marrn salt sobre el perro perezoso."
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

但出于某些安全原因,我不想通过查询字符串传递此API密钥.

为什么我不想使用查询字符串

具有敏感参数的URI不安全,因为它经常被记录或在某些情况下显示,而HTTP Header或HTTP正文则不会.

当然,使用更强大的身份验证方法(服务帐户)对于安全性更好,但API密钥也是一些很好的解决方案,适用于某些用例,例如嵌入在遗留系统中等等.

在JSON请求中传递Api-key时不起作用

我在我的请求JSON中设置了"key"项,但它没有用.它导致授权错误.

curl -H 'Content-Type: application/json' 'https://translation.googleapis.com/language/translate/v2' --data-binary @test.json
Run Code Online (Sandbox Code Playgroud)

与test.json:

{
  "key": "xxxxxxxxxx",
  "q": "The quick brown fox jumped over the lazy dog.",
  "source": "en",
  "target": "es",
  "format": "text"
}
Run Code Online (Sandbox Code Playgroud)

结果:

{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "errors": [
      {
        "message": "The request is missing a valid API key.",
        "domain": "global",
        "reason": "forbidden"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ber 4

我能找到的唯一有效的例子是:

在 URL 中附加密钥,例如:

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

其余的在 POST 中完成。