Google Cloud - 如何在应用程序代码中验证 USER 而不是服务帐户?
Google Cloud - How to authenticate USER instead of a service account in app code?
我可以通过在 GC 控制台中创建和下载 credentials.json 来授权服务帐户。但此选项不适用于实际的人类用户帐户。我的用户帐户具有我想在应用程序中使用的某些角色,并且需要作为该用户帐户进行身份验证。我该怎么做?有没有办法为用户帐户下载类似的 creds.json 文件?
经过一些挖掘并部分感谢@JohnHanley,我设法使用 google-cloud oauth 库代表具有正确权限的人类用户对我的应用程序进行身份验证:
from google.cloud import secretmanager_v1beta1 as secretmanager
from google_auth_oauthlib import flow
SecretManagerAdminCreds = 'credentials.json'
LAUNCH_BROWSER = True
class SecretManagerAdmin:
def __init__(self, project_id: str = "gcp_project_id",
scopes: list = ['https://www.googleapis.com/auth/cloud-platform']):
self._scopes = scopes
self._project_id = project_id
appflow = flow.InstalledAppFlow.from_client_secrets_file(SecretManagerAdminCreds, scopes=self._scopes)
if LAUNCH_BROWSER:
appflow.run_local_server()
else:
appflow.run_console()
authed_creds = appflow.credentials
self.client = secretmanager.SecretManagerServiceClient(credentials=authed_creds)
当我创建我的 SecretManagerAdmin class 实例时,浏览器启动并要求用户登录 google 并确认应用程序的权限,然后 returns 一秒钟一组凭据,然后用于实例化应用程序使用的 Secretmanager 客户端。
我可以通过在 GC 控制台中创建和下载 credentials.json 来授权服务帐户。但此选项不适用于实际的人类用户帐户。我的用户帐户具有我想在应用程序中使用的某些角色,并且需要作为该用户帐户进行身份验证。我该怎么做?有没有办法为用户帐户下载类似的 creds.json 文件?
经过一些挖掘并部分感谢@JohnHanley,我设法使用 google-cloud oauth 库代表具有正确权限的人类用户对我的应用程序进行身份验证:
from google.cloud import secretmanager_v1beta1 as secretmanager
from google_auth_oauthlib import flow
SecretManagerAdminCreds = 'credentials.json'
LAUNCH_BROWSER = True
class SecretManagerAdmin:
def __init__(self, project_id: str = "gcp_project_id",
scopes: list = ['https://www.googleapis.com/auth/cloud-platform']):
self._scopes = scopes
self._project_id = project_id
appflow = flow.InstalledAppFlow.from_client_secrets_file(SecretManagerAdminCreds, scopes=self._scopes)
if LAUNCH_BROWSER:
appflow.run_local_server()
else:
appflow.run_console()
authed_creds = appflow.credentials
self.client = secretmanager.SecretManagerServiceClient(credentials=authed_creds)
当我创建我的 SecretManagerAdmin class 实例时,浏览器启动并要求用户登录 google 并确认应用程序的权限,然后 returns 一秒钟一组凭据,然后用于实例化应用程序使用的 Secretmanager 客户端。