-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
39 lines (29 loc) · 883 Bytes
/
read.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
package read
import (
"context"
"log"
"os"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
func Read() [][]interface{} {
ctx := context.Background()
secrets := os.Getenv("JWT_CONFIG")
config, err := google.JWTConfigFromJSON([]byte(secrets), "https://www.googleapis.com/auth/spreadsheets.readonly")
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := config.Client(ctx)
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve Sheets client: %v", err)
}
spreadsheetId := os.Getenv("SHEET_ID")
readRange := os.Getenv("READ_RANGE")
resp, err := srv.Spreadsheets.Values.Get(spreadsheetId, readRange).Do()
if err != nil {
log.Fatalf("Unable to retrieve data from sheet: %v", err)
}
return resp.Values
}