fix oauth2 storage and reuse
pretty sure this isn't the *best* approach, but it's an approach that works.
This commit is contained in:
parent
45966d40ae
commit
4624c56e54
2 changed files with 30 additions and 16 deletions
32
main.go
32
main.go
|
|
@ -214,24 +214,37 @@ func main() {
|
||||||
|
|
||||||
// youtube oauth flow
|
// youtube oauth flow
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
oauth2Config := &oauth2.Config{
|
||||||
|
ClientID: cfg.Google.ClientID,
|
||||||
|
ClientSecret: cfg.Google.ClientSecret,
|
||||||
|
Endpoint: google.Endpoint,
|
||||||
|
Scopes: []string{ youtube.YoutubeScope },
|
||||||
|
RedirectURL: cfg.RedirectUri,
|
||||||
|
}
|
||||||
var token *oauth2.Token
|
var token *oauth2.Token
|
||||||
if cfg.Token != nil {
|
if cfg.Token != nil {
|
||||||
token = cfg.Token
|
token = cfg.Token
|
||||||
} else {
|
} else {
|
||||||
token, err = generateOAuthToken(&ctx, cfg)
|
token, err = generateOAuthToken(&ctx, oauth2Config, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("OAuth flow failed: %v", err)
|
log.Fatalf("OAuth flow failed: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
cfg.Token = token
|
cfg.Token = token
|
||||||
|
}
|
||||||
|
tokenSource := oauth2Config.TokenSource(ctx, token)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to create OAuth2 token source: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
err = config.WriteConfig(cfg, config.CONFIG_FILENAME)
|
err = config.WriteConfig(cfg, config.CONFIG_FILENAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to save OAuth token: %v", err)
|
log.Fatalf("Failed to save OAuth token: %v", err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// okay actually upload now!
|
// okay actually upload now!
|
||||||
ytVideo, err := yt.UploadVideo(ctx, token, video)
|
ytVideo, err := yt.UploadVideo(ctx, tokenSource, video)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to upload video: %v", err)
|
log.Fatalf("Failed to upload video: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
@ -283,14 +296,11 @@ func initialiseDirectory(directory string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateOAuthToken(ctx *context.Context, cfg *config.Config) (*oauth2.Token, error) {
|
func generateOAuthToken(
|
||||||
oauth2Config := &oauth2.Config{
|
ctx *context.Context,
|
||||||
ClientID: cfg.Google.ClientID,
|
oauth2Config *oauth2.Config,
|
||||||
ClientSecret: cfg.Google.ClientSecret,
|
cfg *config.Config,
|
||||||
Endpoint: google.Endpoint,
|
) (*oauth2.Token, error) {
|
||||||
Scopes: []string{ youtube.YoutubeScope },
|
|
||||||
RedirectURL: cfg.RedirectUri,
|
|
||||||
}
|
|
||||||
verifier := oauth2.GenerateVerifier()
|
verifier := oauth2.GenerateVerifier()
|
||||||
|
|
||||||
var token *oauth2.Token
|
var token *oauth2.Token
|
||||||
|
|
|
||||||
|
|
@ -168,7 +168,11 @@ func BuildDescription(video *Video) (string, error) {
|
||||||
return strings.TrimSpace(out.String()), err
|
return strings.TrimSpace(out.String()), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func UploadVideo(ctx context.Context, token *oauth2.Token, video *Video) (*youtube.Video, error) {
|
func UploadVideo(
|
||||||
|
ctx context.Context,
|
||||||
|
tokenSource oauth2.TokenSource,
|
||||||
|
video *Video,
|
||||||
|
) (*youtube.Video, error) {
|
||||||
title, err := BuildTitle(video)
|
title, err := BuildTitle(video)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to build title: %v", err)
|
return nil, fmt.Errorf("failed to build title: %v", err)
|
||||||
|
|
@ -181,7 +185,7 @@ func UploadVideo(ctx context.Context, token *oauth2.Token, video *Video) (*youtu
|
||||||
service, err := youtube.NewService(
|
service, err := youtube.NewService(
|
||||||
ctx,
|
ctx,
|
||||||
option.WithScopes(youtube.YoutubeUploadScope),
|
option.WithScopes(youtube.YoutubeUploadScope),
|
||||||
option.WithTokenSource(oauth2.StaticTokenSource(token)),
|
option.WithTokenSource(tokenSource),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create youtube service: %v\n", err)
|
log.Fatalf("Failed to create youtube service: %v\n", err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue