-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsheet.go
53 lines (43 loc) · 1.33 KB
/
gsheet.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
package gsheet
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"github.com/frasnym/go-expense-telebot/config"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
var sheetService *sheets.Service
func init() {
cfg := config.GetConfig()
sheetKey := Key{
Type: "service_account",
ProjectID: cfg.GsheetProjectID,
PrivateKeyID: cfg.GsheetUserPrivateKeyID,
PrivateKey: cfg.GsheetUserPrivateKey,
ClientEmail: cfg.GsheetUserClientEmail,
ClientID: cfg.GsheetUserClientID,
AuthURI: "https://accounts.google.com/o/oauth2/auth",
TokenURI: "https://oauth2.googleapis.com/token",
AuthProvider: "https://www.googleapis.com/oauth2/v1/certs",
Client: fmt.Sprintf("https://www.googleapis.com/robot/v1/metadata/x509/%s", url.QueryEscape(cfg.GsheetUserClientEmail)),
UniverseDomain: "googleapis.com",
}
credential, err := json.Marshal(sheetKey)
if err != nil {
panic(fmt.Errorf("failed to get key: %v", err))
}
srv, err := sheets.NewService(context.Background(), option.WithCredentialsJSON(credential))
if err != nil {
panic(fmt.Errorf("unable to retrieve Sheets client: %v", err))
}
sheetService = srv
}
func GetService() *sheets.Service {
if sheetService == nil {
panic(errors.New("please init gsheet service first"))
}
return sheetService
}