Last active
May 12, 2022 14:19
-
-
Save salrashid123/208f3cb925b48b71233e429a7835f8e0 to your computer and use it in GitHub Desktop.
List Groups using Workspace Directory API in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
see https://blog.salrashid.dev/articles/2022/search_group_membership/ | |
*/ | |
import ( | |
"fmt" | |
"log" | |
"context" | |
"golang.org/x/oauth2/google" | |
admin "google.golang.org/api/admin/directory/v1" | |
"google.golang.org/api/option" | |
) | |
func main() { | |
ctx := context.Background() | |
// serviceAccountFile := "/home/srashid/gcp_misc/certs/google_apps_svc_dwd.json" | |
// serviceAccountJSON, err := ioutil.ReadFile(serviceAccountFile) | |
// if err != nil { | |
// log.Fatal(err) | |
// } | |
// config, err := google.JWTConfigFromJSON(serviceAccountJSON, admin.AdminDirectoryUserReadonlyScope, admin.AdminDirectoryGroupReadonlyScope) | |
// if err != nil { | |
// log.Fatal(err) | |
// } | |
// ts := config.TokenSource(ctx) | |
ts, err := google.DefaultTokenSource(ctx) | |
if err != nil { | |
log.Fatal(err) | |
} | |
srv, err := admin.NewService(ctx, option.WithTokenSource(ts)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = srv.Groups.List().Customer("C023zw3x8").Pages(ctx, func(groupsReport *admin.Groups) error { | |
for _, m := range groupsReport.Groups { | |
fmt.Printf("%s (%s)\n", m.Email, m.Id) | |
} | |
return nil | |
}) | |
if err != nil { | |
fmt.Printf("Error %v", err) | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment