change sheet use api key instead of use user credential
This commit is contained in:
parent
8ccbbc8647
commit
41812591b7
4 changed files with 22 additions and 17 deletions
|
|
@ -2,9 +2,9 @@ package sheet
|
|||
|
||||
import (
|
||||
"context"
|
||||
"recipe-manager/helpers"
|
||||
"errors"
|
||||
"recipe-manager/config"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
"google.golang.org/api/option"
|
||||
"google.golang.org/api/sheets/v4"
|
||||
)
|
||||
|
|
@ -14,31 +14,22 @@ type SheetService interface {
|
|||
}
|
||||
|
||||
type sheetService struct {
|
||||
config *oauth2.Config
|
||||
service *sheets.Service
|
||||
}
|
||||
|
||||
func NewSheetService(ctx context.Context) (SheetService, error) {
|
||||
clientSecret, err := helpers.GetClientSecret("client_secret.json")
|
||||
func NewSheetService(ctx context.Context, cfg *config.ServerConfig) (SheetService, error) {
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if cfg.APIKey == "" {
|
||||
return nil, errors.New("API_KEY is empty")
|
||||
}
|
||||
|
||||
token, err := helpers.GetToken("token.json")
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
service, err := sheets.NewService(ctx, option.WithTokenSource(clientSecret.TokenSource(ctx, token)))
|
||||
service, err := sheets.NewService(ctx, option.WithAPIKey(cfg.APIKey))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &sheetService{
|
||||
config: clientSecret,
|
||||
service: service,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue