-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleapi.go
56 lines (43 loc) · 969 Bytes
/
googleapi.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
50
51
52
53
54
55
56
package utils
import (
"context"
"io/ioutil"
"sync"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
const (
spreadsheetID = "1gIOfb8v4GJu1PnMBjxgefQkelb1hyQycQeQjqwFZUvQ"
readRange = "Sheet4!A:C"
credentials = "key.json"
)
type RequestData struct {
Values [][]interface{} `json:"data"`
}
var sheetsService *sheets.Service
var once sync.Once
func initialize() error {
creds, err := ioutil.ReadFile("key.json")
if err != nil {
return err
}
config, err := google.JWTConfigFromJSON(creds, sheets.SpreadsheetsScope)
if err != nil {
return err
}
client := config.Client(context.Background())
srv, err := sheets.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return err
}
sheetsService = srv
return nil
}
func GetSheetsService() (*sheets.Service, error) {
var err error
once.Do(func() {
err = initialize()
})
return sheetsService, err
}