【问题标题】:how to use offical google translate api on local runtime via python如何通过python在本地运行时使用官方谷歌翻译api
【发布时间】:2021-11-30 14:26:33
【问题描述】:

在阅读了官方的google translate api文档后,它为我们提供了以下示例代码:

from google.cloud import translate
def translate_text(text="Hello, world!", project_id="weighty-site-333613"):

    client = translate.TranslationServiceClient().from_service_account_json('key.json')
    location = "global"
    parent = f"projects/{project_id}/locations/{location}"
    response = client.translate_text(
        request={
            "parent": parent,
            "contents": [text],
            "mime_type": "text/plain",
            "source_language_code": "en-US",
            "target_language_code": "zh-CN",
        }
    )

    for translation in response.translations:
        print("Translated text: {}".format(translation.translated_text))
translate_text()

这些代码在谷歌云终端上正常工作。 但是,即使我将“key.json”文件放在同一个文件夹中,也会显示如下错误:

/usr/local/bin/python3.6 /Users/jiajunmao/Documents/GitHub/translator_of_excel/google_trans.py
Traceback (most recent call last):
  File "/Users/jiajunmao/Documents/GitHub/translator_of_excel/google_trans.py", line 37, in <module>
    translate_text()
  File "/Users/jiajunmao/Documents/GitHub/translator_of_excel/google_trans.py", line 22, in translate_text
    client = translate.TranslationServiceClient().from_service_account_json('key.json')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/cloud/translate_v3/services/translation_service/client.py", line 354, in __init__
    always_use_jwt_access=True,
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/cloud/translate_v3/services/translation_service/transports/grpc.py", line 158, in __init__
    always_use_jwt_access=always_use_jwt_access,
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/cloud/translate_v3/services/translation_service/transports/base.py", line 110, in __init__
    **scopes_kwargs, quota_project_id=quota_project_id
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/auth/_default.py", line 488, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started

Process finished with exit code 1

有人能告诉我在这一步我应该做什么吗?非常感谢

【问题讨论】:

  • 项目中有服务帐号文件吗?

标签: python google-api google-translate


【解决方案1】:

您需要在 IAM 和服务帐户下从 GCP 获得正确权限的服务帐户 json 文件。

然后你需要实现命令,

GOOGLE_APPLICATION_CREDENTIALS = "/path/to/your/service_account.json" 

【讨论】:

  • 我可以知道我应该把这个放在哪里吗?
  • 推荐在root,但是因为需要指向全路径,所以可以放在项目的任意位置。
猜你喜欢
  • 2013-03-17
  • 2012-03-13
  • 2017-11-06
  • 2021-03-08
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
相关资源
最近更新 更多