Getting Started¶
Installation¶
Authentication¶
GoGoogle uses OAuth2 for authentication. You'll need:
- A Google Cloud project with APIs enabled
- OAuth2 credentials (client_secret.json)
- User authorization (generates token.json)
Enable Google APIs¶
- Go to Google Cloud Console
- Create or select a project
- Enable the APIs you need:
- Gmail API
- Google Sheets API
- Google Slides API
- etc.
Create OAuth Credentials¶
- Go to APIs & Services > Credentials
- Click Create Credentials > OAuth client ID
- Select Desktop app as application type
- Download the JSON file as
client_secret.json
Authorize Your Application¶
Use goauth for OAuth2 flow:
import (
"context"
"github.com/grokify/goauth/google"
)
client, err := google.NewClientOAuthCLITokenStore(google.ClientOAuthCLITokenStoreConfig{
Context: ctx,
AppConfig: clientSecretJSON, // Contents of client_secret.json
Scopes: []string{
"https://www.googleapis.com/auth/gmail.send",
"https://www.googleapis.com/auth/spreadsheets.readonly",
},
TokenFile: "token.json", // Will be created after authorization
State: "my-app",
})
if err != nil {
log.Fatal(err)
}
On first run, this opens a browser for user authorization. The token is saved to token.json for subsequent runs.
Common OAuth Scopes¶
Gmail¶
| Scope | Description |
|---|---|
gmail.send |
Send emails only |
gmail.readonly |
Read emails only |
gmail.modify |
Read, send, delete emails |
gmail.labels |
Manage labels |
Sheets¶
| Scope | Description |
|---|---|
spreadsheets.readonly |
Read spreadsheets |
spreadsheets |
Read and write spreadsheets |
Slides¶
| Scope | Description |
|---|---|
presentations.readonly |
Read presentations |
presentations |
Read and write presentations |
Create Service Clients¶
After authentication, create service clients:
Gmail¶
import "github.com/grokify/gogoogle/gmailutil/v1"
service, err := gmailutil.NewGmailService(ctx, httpClient)
if err != nil {
log.Fatal(err)
}
Sheets¶
import "google.golang.org/api/sheets/v4"
service, err := sheets.NewService(ctx, option.WithHTTPClient(httpClient))
if err != nil {
log.Fatal(err)
}
Slides¶
import "google.golang.org/api/slides/v1"
service, err := slides.NewService(ctx, option.WithHTTPClient(httpClient))
if err != nil {
log.Fatal(err)
}
Environment Variables¶
For production, store credentials securely:
Next Steps¶
- Gmail Guide - Send emails and manage messages
- Sheets Guide - Read and write spreadsheet data
- CLI Tools - Command-line utilities