简体   繁体   English

如何通过 python 在本地运行时使用官方谷歌翻译 api

[英]how to use offical google translate api on local runtime via python

after reading the offical google translate api document, it provide us with the following sample code:在阅读了官方的谷歌翻译 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()

These code worked properly on google cloud terminal.这些代码在谷歌云终端上正常工作。 However, even if i put the "key.json" file in the same folder, an error like this is shown:但是,即使我将“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

can someone tell me what should i do at this step?有人可以告诉我在这一步我应该做什么吗? thank you so much太感谢了

You need service account json file with correct permissions from GCP under IAM & Service Accounts.您需要服务帐户 json 文件,该文件在 IAM 和服务帐户下具有来自 GCP 的正确权限。

Then you need to implement command,然后你需要执行命令,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM