Back

Step by Step Guide to Building a Google Business Profile API Integration in Go

Aug 1, 20246 minute read

Introduction

Hey there, fellow Go enthusiast! Ready to supercharge your business applications with the Google Business Profile API? You're in for a treat. This powerful API lets you manage business information, locations, and even customer reviews programmatically. Let's dive in and build something awesome!

Prerequisites

Before we jump into the code, make sure you've got these basics covered:

  • Go installed on your machine (I know you probably do, but just checking!)
  • A Google Cloud Console project set up
  • API credentials (OAuth 2.0 client ID) - grab these from your Google Cloud Console

Setting up the project

Let's get our project off the ground:

  1. Create a new Go project:
mkdir gbp-api-integration cd gbp-api-integration go mod init gbp-api-integration
  1. Install the google-api-go-client package:
go get google.golang.org/api/mybusiness/v4

Authentication

Time to tackle OAuth 2.0. Don't worry, it's not as scary as it sounds!

import ( "golang.org/x/oauth2" "golang.org/x/oauth2/google" ) func getClient(config *oauth2.Config) *http.Client { // TODO: Implement token storage and retrieval tok := getTokenFromStorage() return config.Client(context.Background(), tok) }

Pro tip: Store your tokens securely. You don't want to reauthenticate every time you run your app!

Initializing the API client

Let's create our Google Business Profile API service:

import "google.golang.org/api/mybusiness/v4" svc, err := mybusiness.NewService(ctx, option.WithHTTPClient(client)) if err != nil { log.Fatalf("Unable to create service: %v", err) }

Basic API operations

Now for the fun part - let's interact with the API!

Retrieving business information

account, err := svc.Accounts.Get("accounts/123456789").Do() if err != nil { log.Fatalf("Failed to retrieve account: %v", err) } fmt.Printf("Account name: %s\n", account.AccountName)

Updating business details

location := &mybusiness.Location{ StoreCode: "123", Name: "My Awesome Store", } updatedLocation, err := svc.Accounts.Locations.Patch("accounts/123456789/locations/987654321", location).UpdateMask("storeCode,name").Do() if err != nil { log.Fatalf("Failed to update location: %v", err) }

Advanced features

Want to take it up a notch? Let's handle some media and reviews!

Managing photos

media := &mybusiness.MediaItem{ MediaFormat: "PHOTO", SourceUrl: "https://example.com/photo.jpg", } createdMedia, err := svc.Accounts.Locations.Media.Create("accounts/123456789/locations/987654321", media).Do() if err != nil { log.Fatalf("Failed to upload photo: %v", err) }

Managing reviews

review := &mybusiness.ReviewReply{ Comment: "Thank you for your feedback!", } updatedReview, err := svc.Accounts.Locations.Reviews.UpdateReply("accounts/123456789/locations/987654321/reviews/ABC123", review).Do() if err != nil { log.Fatalf("Failed to reply to review: %v", err) }

Error handling and best practices

Always check for errors and respect those API quotas! Here's a quick example:

if err != nil { if e, ok := err.(*googleapi.Error); ok { if e.Code == 429 { // Handle rate limiting time.Sleep(time.Second * 5) // Retry the request } } return err }

Testing and debugging

Don't forget to test your API calls! Here's a simple example:

func TestGetAccount(t *testing.T) { svc := setupTestService() account, err := svc.Accounts.Get("accounts/123456789").Do() if err != nil { t.Errorf("Failed to get account: %v", err) } if account.AccountName == "" { t.Error("Account name is empty") } }

Conclusion

And there you have it! You're now equipped to build a robust Google Business Profile API integration in Go. Remember, the API is constantly evolving, so keep an eye on the official documentation for the latest features and best practices.

Happy coding, and may your businesses profiles always be up-to-date!