-
Notifications
You must be signed in to change notification settings - Fork 6
/
service.go
37 lines (31 loc) · 1.03 KB
/
service.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
package gsheet
import (
"context"
"io/ioutil"
"log"
"os"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
// NewSheetService returns a Sheets service used to communicate with Google Sheets.
func NewSheetService() (*sheets.Service, context.Context) {
ctx := context.Background()
//Os get env variable GOOGLE_APPLICATION_CREDENTIALS
GOOGLE_APPLICATION_CREDENTIALS := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
b, err := ioutil.ReadFile(GOOGLE_APPLICATION_CREDENTIALS)
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
// If modifying these scopes, delete your previously saved token.json.
config, err := google.ConfigFromJSON(b, "https://www.googleapis.com/auth/spreadsheets")
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve Sheets client: %v", err)
}
return srv, ctx
}