【问题标题】:Google Authentication in pythonpython中的谷歌身份验证
【发布时间】:2020-05-01 03:08:39
【问题描述】:

我正在尝试获得身份验证以使用谷歌翻译 API。目前在我的本地机器上我只是这样做:

from google.cloud import translate_v2 as translate

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = path_to_cred_json_file
translate_client = translate.Client()

效果很好。但是,我希望在 AWS 上执行此操作,我将凭证 json 文件存储在 AWS 机密中。在translate.Client 的文档中,我看到了这一点:

Init signature:
translate.Client(
    target_language='en',
    credentials=None,
    ...
)
...

:type credentials: :class:`~google.auth.credentials.Credentials`

但是,如果我读入 json 文件并尝试将其作为凭据参数传入,则会出错。

我目前在 AWS 中的唯一答案是读取密钥,将其写为 json 文件,然后设置 os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = path_to_cred_json_file,这将起作用,但被数据工程师告知存在安全风险。

所以问题是我如何在不读取物理文件的情况下获得这个google.auth.credentials.Credentials 对象。我可以访问内存中 json 文件的纯文本版本(通过 AWS 机密)。总的来说,我对 AWS 很陌生,所以请放轻松。

【问题讨论】:

标签: python amazon-web-services google-authentication aws-secrets-manager


【解决方案1】:

感谢@miles-budnek 和this github 的评论,找到了答案。

假设我将 json 字符串作为字典,称为 secret:

from google.cloud import translate_v2 as translate
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_info(secret) 
t_client = translate.Client(credentials=credentials)

【讨论】:

    猜你喜欢
    • 2012-09-20
    • 2020-08-15
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    相关资源
    最近更新 更多