【问题标题】:query google groups api from gcp instance/function python从 gcp 实例/函数 python 查询 google groups api
【发布时间】: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


    【解决方案1】:

    我从谷歌得到的答案:

    1. 不,如果不为 SA 生成用于模拟的私钥,这是不可能的。
    2. 不,获取组的正确方法是查询 Gsuite 的 API。

    【讨论】:

      【解决方案2】:

      您应该阅读官方 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
      

      【讨论】:

        猜你喜欢
        • 2019-04-14
        • 2021-10-30
        • 1970-01-01
        • 2020-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-12
        相关资源
        最近更新 更多