From 4624c56e54fbdc4103656d0905b0615b17d14514 Mon Sep 17 00:00:00 2001 From: ari melody Date: Fri, 30 Jan 2026 15:55:57 +0000 Subject: [PATCH] fix oauth2 storage and reuse pretty sure this isn't the *best* approach, but it's an approach that works. --- main.go | 38 ++++++++++++++++++++++++-------------- youtube/youtube.go | 8 ++++++-- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 261fd85..05e1dad 100644 --- a/main.go +++ b/main.go @@ -214,24 +214,37 @@ func main() { // youtube oauth flow 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 if cfg.Token != nil { token = cfg.Token } else { - token, err = generateOAuthToken(&ctx, cfg) + token, err = generateOAuthToken(&ctx, oauth2Config, cfg) if err != nil { log.Fatalf("OAuth flow failed: %v", err) os.Exit(1) } cfg.Token = token - err = config.WriteConfig(cfg, config.CONFIG_FILENAME) - if err != nil { - log.Fatalf("Failed to save OAuth token: %v", err) - } + } + 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) + if err != nil { + log.Fatalf("Failed to save OAuth token: %v", err) } // okay actually upload now! - ytVideo, err := yt.UploadVideo(ctx, token, video) + ytVideo, err := yt.UploadVideo(ctx, tokenSource, video) if err != nil { log.Fatalf("Failed to upload video: %v", err) os.Exit(1) @@ -283,14 +296,11 @@ func initialiseDirectory(directory string) error { return err } -func generateOAuthToken(ctx *context.Context, cfg *config.Config) (*oauth2.Token, error) { - oauth2Config := &oauth2.Config{ - ClientID: cfg.Google.ClientID, - ClientSecret: cfg.Google.ClientSecret, - Endpoint: google.Endpoint, - Scopes: []string{ youtube.YoutubeScope }, - RedirectURL: cfg.RedirectUri, - } +func generateOAuthToken( + ctx *context.Context, + oauth2Config *oauth2.Config, + cfg *config.Config, +) (*oauth2.Token, error) { verifier := oauth2.GenerateVerifier() var token *oauth2.Token diff --git a/youtube/youtube.go b/youtube/youtube.go index 4e50d4c..e7da0f5 100644 --- a/youtube/youtube.go +++ b/youtube/youtube.go @@ -168,7 +168,11 @@ func BuildDescription(video *Video) (string, error) { 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) if err != nil { 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( ctx, option.WithScopes(youtube.YoutubeUploadScope), - option.WithTokenSource(oauth2.StaticTokenSource(token)), + option.WithTokenSource(tokenSource), ) if err != nil { log.Fatalf("Failed to create youtube service: %v\n", err)