首页 > 解决方案 > 谷歌云翻译 API 问题

问题描述

我是堆栈溢出的新手。如果我在这里做任何违反规则的事情,请原谅我。

我正在尝试使用 Google Cloud Translation API,并且完全按照QuickStart中描述的步骤进行操作。

当我运行 curl 命令时,响应表明我没有有效的 API 密钥。请参阅下面我收到的确切回复。

{

  "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"

  }

}

我再次查看了链接,但我相信链接中的任何步骤都没有提到 API 密钥。该链接提到了服务帐户密钥。并且我在环境变量上正确设置了服务帐户私钥的路径,并且没有报告错误。

谁能帮助我了解我应该如何设置 API 密钥?

提前致谢。

环境:Win Powershell 5.1.18362.752

标签: google-cloud-platformgoogle-apigoogle-translation-api

解决方案


您遵循的文档建议使用服务帐户通过 Translation API 进行身份验证。为此,您需要执行以下步骤:

我已经测试过并且有效。

curl -s -X POST -H "Content-Type: application/json"     -H "Authorization: Bearer "$(gcloud auth application-default print-access-token)     --data "{                       
  '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'
}" "https://translation.googleapis.com/language/translate/v2"

另一方面,可以在请求中使用 API 密钥,如此处所示,但需要事先生成它。

curl -s -X POST -H "Content-Type: application/json" \ \
    --data "{
  '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'
}" "https://translation.googleapis.com/language/translate/v2?key=XXXXXXXXXXXXXXXXXXXXXXXXXX"

推荐阅读