google-cloud-platform - 谷歌云翻译 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
解决方案
您遵循的文档建议使用服务帐户通过 Translation API 进行身份验证。为此,您需要执行以下步骤:
- 为您的项目启用 Cloud Translation API,创建服务帐号并下载 JSON 格式的私钥
- 在 Windows Env 中配置 GOOGLE_APPLICATION_CREDENTIALS。这可能会导致问题,因为如果未设置,则请求可能正在搜索 API 密钥,即不同的身份验证方法。
- 设置 JSON 凭证文件后,请再试一次 curl 命令。
我已经测试过并且有效。
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"
推荐阅读
- fiware-cosmos - cosmos-auth 无法连接到 fiware-idm
- ios - 带有 URLSessionStreamTask 的 HTTP2 协议
- ruby-on-rails - 尽管已建立路线,但没有路线匹配
- unity3d - 如何使用 Photon (unity) 用 SteamVR 表示手预制件?
- laravel - Laravel 无法检索正确的当前页码。哪个结果分页不起作用(仅显示第一页结果)
- java - 有没有办法在数学上找到正确的节点?
- ionic-framework - 如何在ionic 3中检测键盘是本机键盘还是第三方键盘?
- sql - SQL 新手。想将 IF(COUNTIFS()) Excel 公式转换为 SQL 代码并让 SQL 计算它而不是 Excel
- python - 使用两个模型进行全文搜索
- javascript - 下拉菜单上已经选择的选项应该是彩色的