-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
64 lines (52 loc) · 1.37 KB
/
common.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
package pkg
import (
"context"
"time"
"github.com/kelseyhightower/envconfig"
"go.uber.org/zap"
"golang.org/x/oauth2"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
type config struct {
GoogleClientID string `envconfig:"GOOGLE_CLIENT_ID" required:"true"`
GoogleClientSecret string `envconfig:"GOOGLE_CLIENT_SECRET" required:"true"`
}
var (
logger *zap.Logger
cfg config
oauthConfig = &oauth2.Config{
ClientID: "", // filled by config
ClientSecret: "", // filled by config
Endpoint: oauth2.Endpoint{
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://oauth2.googleapis.com/token",
},
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
Scopes: []string{youtube.YoutubeReadonlyScope},
}
)
func init() {
var err error
logger, err = zap.NewProduction()
if err != nil {
panic(err)
}
err = envconfig.Process("", &cfg)
if err != nil {
logger.Fatal("failure reading config", zap.Error(err))
}
oauthConfig.ClientID = cfg.GoogleClientID
oauthConfig.ClientSecret = cfg.GoogleClientSecret
}
func ytServiceFromRefreshToken(ctx context.Context, refreshToken string) (*youtube.Service, error) {
oauthToken := &oauth2.Token{
Expiry: time.Now(),
TokenType: "Bearer",
RefreshToken: refreshToken,
}
return youtube.NewService(
ctx,
option.WithTokenSource(oauthConfig.TokenSource(ctx, oauthToken)),
)
}