gpt4 book ai didi

go - 使用 Google Cloud Platform 服务帐号查询 GSuite Directory API

转载 作者:行者123 更新时间:2023-12-01 19:39:56 25 4
gpt4 key购买 nike

我要查询GSuite Admin SDK Directory API返回组中的所有用户、Go 和 认证为 GCP 服务帐号 (该脚本将在 Google Compute Engine VM 中执行或作为 Google Cloud Function 执行)。

我使用的服务帐户(我们称之为 my-service-account@my-project.iam.gserviceaccount.com)已在 GSuite 中获得了必要的范围:

  • https://www.googleapis.com/auth/admin.directory.group.member.readonly
  • https://www.googleapis.com/auth/admin.directory.group.member
  • https://www.googleapis.com/auth/admin.directory.group.readonly
  • https://www.googleapis.com/auth/admin.directory.group

  • 我也有 G Suite 供我使用管理员 帐户(我们称之为 my-admin@my-domain.com )。此帐户将用于 代表团 (见 the docs on delegation)。

    我能够使用以下代码返回组中的所有用户(基于上面提供的链接中的代码,并且我已经删除了大部分错误处理以使代码更短):
    package main

    import (
    "io/ioutil"
    "log"
    "os"

    "golang.org/x/net/context"
    "golang.org/x/oauth2/google"
    admin "google.golang.org/api/admin/directory/v1"
    )

    func main() {
    srv := createAdminDirectoryService(
    os.Getenv("SERVICE_ACCOUNT_FILE_PATH"),
    os.Getenv("GSUITE_ADMIN_USER_EMAIL"),
    )
    members := listUsersInGroup(srv, os.Args[1])
    log.Println(members)
    }

    func createAdminDirectoryService(serviceAccountFilePath,
    gsuiteAdminUserEmail string) *admin.Service {
    jsonCredentials, _ := ioutil.ReadFile(serviceAccountFilePath)

    config, _ := google.JWTConfigFromJSON(
    jsonCredentials,
    admin.AdminDirectoryGroupMemberReadonlyScope,
    )
    config.Subject = gsuiteAdminUserEmail

    ctx := context.Background()
    client := config.Client(ctx)

    srv, _ := admin.New(client)
    return srv
    }

    func listUsersInGroup(srv *admin.Service, groupEmail string) []string {
    members, err := srv.Members.List(groupEmail).Do()
    if err != nil {
    log.Fatal(err)
    }

    membersEmails := make([]string, len(members.Members))
    for i, member := range members.Members {
    membersEmails[i] = member.Email
    }

    return membersEmails
    }

    如您所见,该代码 需要有一个 JSON key 文件 my-service-account@my-project.iam.gserviceaccount.com .这是我发现创建 jwt.Config 的唯一方法目的。

    此外,请注意,委托(delegate)是通过 config.Subject = gsuiteAdminUserEmail 行完成的。 ;没有那个,我得到了错误:
    googleapi: Error 403: Insufficient Permission: Request had insufficient authentication scopes., insufficientPermissions

    无论如何,运行:
    SERVICE_ACCOUNT_FILE_PATH=/path/to/json/key/of/my/service/account \
    GSUITE_ADMIN_USER_EMAIL=my-admin@my-domain.com \
    go run main.go my-group@my-domain.com

    成功打印 my-group@my-domain.com 中的所有用户.

    然而 ,因为此代码将从具有 my-service-account@my-project.iam.gserviceaccount.com 的 Google Compute Engine VM(或 Google Cloud Function)运行用作服务帐户时,需要该服务帐户的 JSON key 进行身份验证似乎很荒谬(因为我已经在 VM 上或 GCF 内通过 my-service-account@my-project.iam.gserviceaccount.com 进行身份验证)。

    我试图替换 createAdminDirectoryService()的内容使用以下代码以启动脚本的用户身份进行身份验证(使用默认凭据):
    func createAdminDirectoryService() *admin.Service {
    ctx := context.Background()

    client, _ := google.DefaultClient(ctx, scopes...)

    srv, _ := admin.New(client)
    return srv
    }

    但是列出用户会返回错误:
    googleapi: Error 403: Insufficient Permission: Request had insufficient authentication scopes., insufficientPermissions

    由于这是我在删除委托(delegate)时遇到的相同错误,我认为这是相关的。事实上,在 admin.Service 期间,我没有在任何地方提供我的 GSuite 管理员帐户。创建。

    任何人都可以帮助解决这些问题之一:
  • 如何直接向在 Google Compute Engine VM 或 Google Cloud Function 上运行 go 脚本的用户进行身份验证?
  • 我真的需要 JSON key 文件来生成 jwt.Config目的?
  • 我查看了 golang.org/x/oauth2/google 的源代码, 我可以得到 oauth2.Config而不是 jwt.Config ,但似乎不可能使用 oauth2 token “委托(delegate)”。我还能如何执行委派?
  • 最佳答案

    这篇文章有点旧,但希望这可以帮助:
    我遇到了同样的问题,并通过更改计算实例中的 OAuth 范围使其工作(默认情况下,范围仅包括 https://www.googleapis.com/auth/cloud-platform )。就我而言,我必须添加 https://www.googleapis.com/auth/admin.directory.group.readonly范围。可以使用 gcloud compute instances set-service-account 来完成命令。这样,从元数据服务器接收到的服务帐户不记名 token 具有访问管理目录 API 的正确范围。
    不幸的是,云功能无法做到这一点,因为云功能 OAuth 范围似乎不可自定义。
    this post更多细节。
    顺便说一句,现在也可以授予服务帐户访问 Admin Directory API without domain-wide delegation 的权限.

    关于go - 使用 Google Cloud Platform 服务帐号查询 GSuite Directory API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55867150/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com