我正在尝试创建一个脚本来从 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 中调用一些?