-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcp.go
72 lines (57 loc) · 1.87 KB
/
gcp.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"context"
"encoding/base64"
"log"
"os"
"github.com/joho/godotenv"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/sheets/v4"
)
func setUpGoogleSheetsAPI() *sheets.Service {
// load .env file from given path
// we keep it empty it will load .env from current directory
err := godotenv.Load(".env")
if err != nil {
log.Println("Warning: .env file not found. Falling back to environment variables.")
}
// load b64 encoded GCP Sheets api credentials
encodedCredentials := os.Getenv("GCP_SHEETS_CREDENTIALS_BASE64")
decodedCredentials, err := base64.StdEncoding.DecodeString(encodedCredentials)
if err != nil {
log.Fatalf("Failed to decode base64 credentials: %v", err)
}
config, err := google.JWTConfigFromJSON(decodedCredentials, sheets.SpreadsheetsReadonlyScope)
if err != nil {
log.Fatalf("Failed to parse credentials: %v", err)
}
ctx := context.Background()
// Use service account or OAuth2 credentials
client := config.Client(ctx)
sheetsService, err := sheets.New(client)
if err != nil {
log.Fatalf("Unable to create Sheets service: %v", err)
}
return sheetsService
}
var (
jwtSecret = []byte(os.Getenv("JWT_SECRET"))
)
func setupGoogleOAuth() *oauth2.Config {
// load .env file from given path
// we keep it empty it will load .env from current directory
err := godotenv.Load(".env")
if err != nil {
log.Println("Warning: .env file not found. Falling back to environment variables.")
}
// Replace with your own Google OAuth2 credentials
oauthConfig := &oauth2.Config{
ClientID: os.Getenv("GCP_OAUTH_CLIENT"),
ClientSecret: os.Getenv("GCP_OAUTH_SECRET"),
RedirectURL: os.Getenv("GCP_OAUTH_REDIRECT_URL"),
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"},
Endpoint: google.Endpoint,
}
return oauthConfig
}