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!
Before we jump into the code, make sure you've got these basics covered:
Let's get our project off the ground:
mkdir gbp-api-integration cd gbp-api-integration go mod init gbp-api-integration
go get google.golang.org/api/mybusiness/v4
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!
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) }
Now for the fun part - let's interact with the API!
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)
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) }
Want to take it up a notch? Let's handle some media and reviews!
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) }
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) }
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 }
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") } }
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!