Helper tool for stitching together livestream VOD segments and uploading them to YouTube!

fix oauth2 storage and reuse

pretty sure this isn't the *best* approach, but it's an approach that
works.

+30 -16
+24 -14
main.go
··· 214 214 215 215 // youtube oauth flow 216 216 ctx := context.Background() 217 + oauth2Config := &oauth2.Config{ 218 + ClientID: cfg.Google.ClientID, 219 + ClientSecret: cfg.Google.ClientSecret, 220 + Endpoint: google.Endpoint, 221 + Scopes: []string{ youtube.YoutubeScope }, 222 + RedirectURL: cfg.RedirectUri, 223 + } 217 224 var token *oauth2.Token 218 225 if cfg.Token != nil { 219 226 token = cfg.Token 220 227 } else { 221 - token, err = generateOAuthToken(&ctx, cfg) 228 + token, err = generateOAuthToken(&ctx, oauth2Config, cfg) 222 229 if err != nil { 223 230 log.Fatalf("OAuth flow failed: %v", err) 224 231 os.Exit(1) 225 232 } 226 233 cfg.Token = token 227 - err = config.WriteConfig(cfg, config.CONFIG_FILENAME) 228 - if err != nil { 229 - log.Fatalf("Failed to save OAuth token: %v", err) 230 - } 234 + } 235 + tokenSource := oauth2Config.TokenSource(ctx, token) 236 + if err != nil { 237 + log.Fatalf("Failed to create OAuth2 token source: %v", err) 238 + os.Exit(1) 239 + } 240 + 241 + err = config.WriteConfig(cfg, config.CONFIG_FILENAME) 242 + if err != nil { 243 + log.Fatalf("Failed to save OAuth token: %v", err) 231 244 } 232 245 233 246 // okay actually upload now! 234 - ytVideo, err := yt.UploadVideo(ctx, token, video) 247 + ytVideo, err := yt.UploadVideo(ctx, tokenSource, video) 235 248 if err != nil { 236 249 log.Fatalf("Failed to upload video: %v", err) 237 250 os.Exit(1) ··· 283 296 return err 284 297 } 285 298 286 - func generateOAuthToken(ctx *context.Context, cfg *config.Config) (*oauth2.Token, error) { 287 - oauth2Config := &oauth2.Config{ 288 - ClientID: cfg.Google.ClientID, 289 - ClientSecret: cfg.Google.ClientSecret, 290 - Endpoint: google.Endpoint, 291 - Scopes: []string{ youtube.YoutubeScope }, 292 - RedirectURL: cfg.RedirectUri, 293 - } 299 + func generateOAuthToken( 300 + ctx *context.Context, 301 + oauth2Config *oauth2.Config, 302 + cfg *config.Config, 303 + ) (*oauth2.Token, error) { 294 304 verifier := oauth2.GenerateVerifier() 295 305 296 306 var token *oauth2.Token
+6 -2
youtube/youtube.go
··· 168 168 return strings.TrimSpace(out.String()), err 169 169 } 170 170 171 - func UploadVideo(ctx context.Context, token *oauth2.Token, video *Video) (*youtube.Video, error) { 171 + func UploadVideo( 172 + ctx context.Context, 173 + tokenSource oauth2.TokenSource, 174 + video *Video, 175 + ) (*youtube.Video, error) { 172 176 title, err := BuildTitle(video) 173 177 if err != nil { 174 178 return nil, fmt.Errorf("failed to build title: %v", err) ··· 181 185 service, err := youtube.NewService( 182 186 ctx, 183 187 option.WithScopes(youtube.YoutubeUploadScope), 184 - option.WithTokenSource(oauth2.StaticTokenSource(token)), 188 + option.WithTokenSource(tokenSource), 185 189 ) 186 190 if err != nil { 187 191 log.Fatalf("Failed to create youtube service: %v\n", err)