-
Notifications
You must be signed in to change notification settings - Fork 100
/
oauth.go
49 lines (45 loc) · 1.23 KB
/
oauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package snippets
import (
"context"
"log"
"golang.org/x/oauth2/google"
"google.golang.org/api/drive/v2"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
"google.golang.org/api/slides/v1"
)
// A group of Google services.
type Services struct {
Drive *drive.Service
Slides *slides.Service
Sheets *sheets.Service
}
// Gets Google services authenticated with Drive, Slides, and Sheets.
func getServices() *Services {
ctx := context.Background()
// Uses env GOOGLE_APPLICATION_CREDENTIALS
client, err := google.DefaultClient(ctx,
drive.DriveScope,
slides.PresentationsScope,
sheets.SpreadsheetsScope)
if err != nil {
log.Fatalf("Error creating Google client: %v", err)
}
driveService, err := drive.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Error creating Drive client: %v", err)
}
slidesService, err := slides.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Error creating Slides client: %v", err)
}
sheetsService, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Error creating Sheets client: %v", err)
}
return &Services{
Drive: driveService,
Slides: slidesService,
Sheets: sheetsService,
}
}