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:
ari melody 2026-01-30 15:55:57 +00:00
parent 45966d40ae
commit 4624c56e54
Signed by: ari
GPG key ID: CF99829C92678188
2 changed files with 30 additions and 16 deletions

38
main.go
View file

@ -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