【发布时间】:2020-05-19 20:25:54
【问题描述】:
我正在尝试创建一个脚本来从 GCP 实例查询 google groups API。该实例附加了 SA,此 SA 具有 SCOPE - GSuite 中允许使用“https://www.googleapis.com/auth/admin.directory.group.readonly”,并且用户还在 GSuite 中设置了附加的自定义角色(列表组)。
对于 SA,我在 GCP 控制台中创建了一个密钥文件。然后我得到凭证,因为文档说:
from googleapiclient.discovery import build
from google.oauth2 import service_account
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
然后添加用户 - 充当。
creds = creds.with_subject('user@domain.com')
service = build('admin', 'directory_v1', credentials=creds)
results = service.groups().list(domain=tenant, maxResults=10,
orderBy='email',
query='email:{}*'.format(group_name)).execute()
然后我查询 API,一切正常,我得到了组。
所以我的问题是: 有没有办法在不生成 json 密钥文件的情况下使用附加到实例的 SA。比如从实例元数据中获取 compute_instance / 默认凭据 / 然后以某种方式向 GSuite API 验证它们?
或者有没有办法在不使用 Gsuite API 的情况下查询组,只需从 GCP 中调用一些?
【问题讨论】:
标签: python google-cloud-platform google-workspace service-accounts