maildownloader/main.go

52 lines
1.1 KiB
Go

package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"golang.org/x/oauth2/google"
"google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
)
const CREDENTIALS_DIR = "creds"
const TOKEN_FILE = "token.json"
const CREDENTIALS_FILE = "credentials.json"
func main() {
ctx := context.Background()
creds_file := filepath.Join(CREDENTIALS_DIR, CREDENTIALS_FILE)
creds_b, err := os.ReadFile(creds_file)
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
config, err := google.ConfigFromJSON(creds_b, gmail.GmailReadonlyScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)
service, err := gmail.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve Gmail client: %v", err)
}
user := "me"
r, err := service.Users.Labels.List(user).Do()
if err != nil {
log.Fatalf("Unable to retrieve labels: %v", err)
}
if len(r.Labels) == 0 {
fmt.Println("No labels found")
return
}
fmt.Println("Labels:")
for _, label := range r.Labels {
fmt.Printf("- %s\n", label.Name)
}
}