从gcp实例/函数python查询google groups api

2024-06-16 14:50:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个脚本,从GCP实例查询google groups API。实例已附加SA,此SA在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_实例/默认凭据/然后以某种方式向gsuiteapi进行身份验证

或者有没有一种方法可以在不点击GSuiteAPI的情况下查询组,只需从GCP中调用一些


Tags: 实例fromcomapiadminservicegooglesa
2条回答

我从谷歌得到的答案是:

  1. 不,不为SA生成私钥进行模拟是不可能的
  2. 不,获取组的正确方法是查询Gsuite的API

您应该阅读官方GCP文档页面上的next文章

下面是一个如何将服务帐户绑定到VM的示例

gcloud compute instances create example-vm \
 service-account my-sa@my-project.iam.gserviceaccount.com \
 scopes https://www.googleapis.com/auth/admin.directory.group.readonly

相关问题 更多 >