Google python 中的身份验证
Google Authentication in python
我正在尝试获取身份验证以使用 google 翻译 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 的新手,所以请放轻松。
感谢@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)
我正在尝试获取身份验证以使用 google 翻译 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 的新手,所以请放轻松。
感谢@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)