使用gcloud auth ...您可以添加或删除gcloud命令期间使用的帐户.
有没有办法在没有grep-ing和awk-ing的情况下获得活动帐户?
gcloud auth list对人类有益,但对机器不够好.我想要更清洁的解决方案.
gcloud config list account 还告诉我详细输出:
Your active configuration is: [default]
[core]
account = service@<my_project>.iam.gserviceaccount.com
Run Code Online (Sandbox Code Playgroud) 我没有使用 JSON 密钥。我使用创建任何新客户端时使用的默认凭据加载机制。
但有什么方法可以反思当前的信用呢?
import google.auth
creds, project = google.auth.default()
# I can get the project id
print(project)
# I can get the oauth stuff
print(creds.client_secret)
print(creds.client_id)
Run Code Online (Sandbox Code Playgroud)
但是如何获取当前活动的用户或 Google 服务帐户的电子邮件名称呢?
我试过这个
print(creds.service_account_email)
Run Code Online (Sandbox Code Playgroud)
但它返回一个错误:
AttributeError: 'Credentials' object has no attribute 'service_account_email'
Run Code Online (Sandbox Code Playgroud)
这应该是该类的属性。
我想获取用户或谷歌服务帐户的电子邮件名称。
与此文章相关:以编程方式获取 GCP 上的当前服务帐户
从该帖子的示例中,如果不是 GSA,是否有办法获取用户的电子邮件名称?
import google.auth
credentials, project_id = google.auth.default()
if hasattr(credentials, "service_account_email"):
print(credentials.service_account_email)
else:
print("WARNING: no service account credential. User account credential?")
Run Code Online (Sandbox Code Playgroud)