Last active
March 27, 2019 02:18
-
-
Save DazWilkin/afb0413a25272dc7d855ebec5fcadcb6 to your computer and use it in GitHub Desktop.
Stackoverflow #55345991
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 | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"golang.org/x/net/context" | |
"golang.org/x/oauth2/google" | |
"google.golang.org/api/admin/directory/v1" | |
) | |
func main() { | |
serviceAccountFile := "./credentials.json" | |
serviceAccountJSON, err := ioutil.ReadFile(serviceAccountFile) | |
if err != nil { | |
log.Fatal(err) | |
} | |
config, err := google.JWTConfigFromJSON(serviceAccountJSON, | |
admin.AdminDirectoryUserScope, | |
) | |
config.Subject = "[[ADMIN-USER]]@[[DOMAIN]]" | |
srv, err := admin.New(config.Client(context.Background())) | |
if err != nil { | |
log.Fatal(err) | |
} | |
usersReport, err := srv.Users.List().Customer("[[IDPID]]").MaxResults(10).OrderBy("email").Do() | |
if err != nil { | |
log.Fatal(err) | |
} | |
if len(usersReport.Users) == 0 { | |
fmt.Print("No users found.\n") | |
} else { | |
fmt.Print("Users:\n") | |
for _, u := range usersReport.Users { | |
fmt.Printf("%s (%s)\n", u.PrimaryEmail, u.Name.FullName) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment