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
|
|
@ -5,5 +5,6 @@ type ServerConfig struct {
|
||||||
AllowedOrigins string `mapstructure:"ALLOWED_ORIGINS"`
|
AllowedOrigins string `mapstructure:"ALLOWED_ORIGINS"`
|
||||||
ClientRedirectURL string `mapstructure:"CLIENT_REDIRECT_URL"`
|
ClientRedirectURL string `mapstructure:"CLIENT_REDIRECT_URL"`
|
||||||
ServerDomain string `mapstructure:"SERVER_DOMAIN"`
|
ServerDomain string `mapstructure:"SERVER_DOMAIN"`
|
||||||
|
APIKey string `mapstructure:"API_KEY"`
|
||||||
Debug bool `mapstructure:"DEBUG_MODE"`
|
Debug bool `mapstructure:"DEBUG_MODE"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,20 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
||||||
|
|
||||||
r.Get("/test/sheet", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/test/sheet", func(w http.ResponseWriter, r *http.Request) {
|
||||||
result := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE")
|
result := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE")
|
||||||
json.NewEncoder(w).Encode(result)
|
|
||||||
|
mapResult := []map[string]string{}
|
||||||
|
|
||||||
|
for _, v := range result {
|
||||||
|
mapResult = append(mapResult, map[string]string{
|
||||||
|
"product_code": v[0].(string),
|
||||||
|
"name": v[1].(string),
|
||||||
|
"other_name": v[2].(string),
|
||||||
|
"description": v[3].(string),
|
||||||
|
"other_description": v[4].(string),
|
||||||
|
"image": v[5].(string),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
json.NewEncoder(w).Encode(mapResult)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -438,7 +438,7 @@ func (s *Server) createHandler() {
|
||||||
})
|
})
|
||||||
|
|
||||||
// Recipe Router
|
// Recipe Router
|
||||||
sheetService, err := sheet.NewSheetService(context.Background())
|
sheetService, err := sheet.NewSheetService(context.Background(), s.cfg)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Fatal("Error while trying to create sheet service: ", zap.Error(err))
|
Log.Fatal("Error while trying to create sheet service: ", zap.Error(err))
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@ package sheet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"recipe-manager/helpers"
|
"errors"
|
||||||
|
"recipe-manager/config"
|
||||||
|
|
||||||
"golang.org/x/oauth2"
|
|
||||||
"google.golang.org/api/option"
|
"google.golang.org/api/option"
|
||||||
"google.golang.org/api/sheets/v4"
|
"google.golang.org/api/sheets/v4"
|
||||||
)
|
)
|
||||||
|
|
@ -14,31 +14,22 @@ type SheetService interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type sheetService struct {
|
type sheetService struct {
|
||||||
config *oauth2.Config
|
|
||||||
service *sheets.Service
|
service *sheets.Service
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSheetService(ctx context.Context) (SheetService, error) {
|
func NewSheetService(ctx context.Context, cfg *config.ServerConfig) (SheetService, error) {
|
||||||
clientSecret, err := helpers.GetClientSecret("client_secret.json")
|
|
||||||
|
|
||||||
if err != nil {
|
if cfg.APIKey == "" {
|
||||||
return nil, err
|
return nil, errors.New("API_KEY is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := helpers.GetToken("token.json")
|
service, err := sheets.NewService(ctx, option.WithAPIKey(cfg.APIKey))
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
service, err := sheets.NewService(ctx, option.WithTokenSource(clientSecret.TokenSource(ctx, token)))
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &sheetService{
|
return &sheetService{
|
||||||
config: clientSecret,
|
|
||||||
service: service,
|
service: service,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue