Back

Step by Step Guide to Building a Google Workspace Admin API Integration in Go

Aug 3, 20245 minute read

Introduction

Hey there, fellow Go enthusiast! Ready to dive into the world of Google Workspace Admin API? We're going to build something cool using the google.golang.org/api/admin/directory/v1 package. Buckle up, because this ride is going to be smooth and exciting!

Prerequisites

Before we jump in, make sure you've got:

  • Go installed (I know you do, but just checking!)
  • A Google Cloud project set up
  • The right credentials and permissions (you know the drill)

Setting up the project

Let's get our hands dirty:

mkdir workspace-admin-api && cd workspace-admin-api go mod init workspace-admin-api go get google.golang.org/api/admin/directory/v1

Authentication

Time to make friends with Google:

  1. Create a service account in your Google Cloud Console
  2. Download the JSON credentials
  3. Now, let's authenticate in Go:
import ( "golang.org/x/oauth2/google" admin "google.golang.org/api/admin/directory/v1" ) credentials, err := google.CredentialsFromJSON(context.Background(), []byte(jsonKey), admin.AdminDirectoryUserScope) if err != nil { log.Fatal(err) }

Initializing the Admin SDK client

Let's bring our client to life:

adminService, err := admin.New(oauth2.NewClient(context.Background(), credentials.TokenSource)) if err != nil { log.Fatal(err) }

Making API requests

Now for the fun part - let's play with some data!

Listing users

users, err := adminService.Users.List().Customer("my_customer").Do() if err != nil { log.Fatal(err) } for _, user := range users.Users { fmt.Printf("User: %s (%s)\n", user.Name.FullName, user.PrimaryEmail) }

Creating a new user

newUser := &admin.User{ PrimaryEmail: "[email protected]", Name: &admin.UserName{ GivenName: "New", FamilyName: "User", }, Password: "temporaryPassword123!", } user, err := adminService.Users.Insert(newUser).Do() if err != nil { log.Fatal(err) } fmt.Printf("Created user: %s\n", user.PrimaryEmail)

Handling responses and errors

Always be prepared:

if err != nil { if e, ok := err.(*googleapi.Error); ok { fmt.Printf("Error code: %v\n", e.Code) fmt.Printf("Message: %v\n", e.Message) } else { fmt.Printf("Error: %v\n", err) } return }

Pagination and performance considerations

Don't let large datasets slow you down:

pageToken := "" for { users, err := adminService.Users.List().Customer("my_customer").PageToken(pageToken).Do() if err != nil { log.Fatal(err) } for _, user := range users.Users { // Process each user } if users.NextPageToken == "" { break } pageToken = users.NextPageToken }

Advanced topics

Want to level up? Look into batch requests and webhook integration. They're game-changers!

Conclusion

And there you have it! You've just built a Google Workspace Admin API integration in Go. Pretty cool, right? Remember, this is just the tip of the iceberg. There's so much more you can do with this API.

For more in-depth info, check out the official documentation.

Happy coding, and may your Go routines be ever in your favor!

Code repository

Find the complete example code on GitHub. Fork it, star it, make it your own!