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
|
||||
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
|
||||
}
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue