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"`
|
||||
ClientRedirectURL string `mapstructure:"CLIENT_REDIRECT_URL"`
|
||||
ServerDomain string `mapstructure:"SERVER_DOMAIN"`
|
||||
APIKey string `mapstructure:"API_KEY"`
|
||||
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) {
|
||||
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
|
||||
sheetService, err := sheet.NewSheetService(context.Background())
|
||||
sheetService, err := sheet.NewSheetService(context.Background(), s.cfg)
|
||||
|
||||
if err != nil {
|
||||
Log.Fatal("Error while trying to create sheet service: ", zap.Error(err))
|
||||
|
|
|
|||
|
|
@ -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