From 110c852301bfedef71d8a089781e64d4c2dcb780 Mon Sep 17 00:00:00 2001 From: OpenWaygate <27287694+OpenWaygate@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:17:25 +0800 Subject: [PATCH] :children_crossing: Prevent re-auth when scope changed --- pkg/auth/auth.go | 3 ++- pkg/yutuber/channel.go | 10 ++++------ pkg/yutuber/playlist.go | 3 --- pkg/yutuber/video.go | 4 ---- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index e565a9f..7269e72 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -45,8 +45,9 @@ For more information about the client_secrets.json file format, please visit: https://developers.google.com/api-client-library/python/guide/aaa_client_secrets ` -func NewY2BService(scope string) *youtube.Service { +func NewY2BService() *youtube.Service { ctx := context.Background() + scope := youtube.YoutubeScope client := getClient(ctx, scope) service, err := youtube.NewService(ctx, option.WithHTTPClient(client)) if err != nil { diff --git a/pkg/yutuber/channel.go b/pkg/yutuber/channel.go index 8ab1191..b68452d 100644 --- a/pkg/yutuber/channel.go +++ b/pkg/yutuber/channel.go @@ -11,8 +11,10 @@ import ( ) var ( - errGetChannel error = errors.New("failed to get channel") - errUpdateChannel error = errors.New("failed to update channel") + service *youtube.Service = auth.NewY2BService() + part []string = []string{"snippet", "statistics"} + errGetChannel error = errors.New("failed to get channel") + errUpdateChannel error = errors.New("failed to update channel") ) type Channel struct { @@ -22,8 +24,6 @@ type Channel struct { user string } -var part = []string{"snippet", "statistics"} - type ChannelService interface { List() Update() @@ -66,7 +66,6 @@ func (c *Channel) Update() { channel.Snippet.Description = c.desc } - service := auth.NewY2BService(youtube.YoutubeScope) call := service.Channels.Update(part, channel) _, err := call.Do() if err != nil { @@ -77,7 +76,6 @@ func (c *Channel) Update() { } func (c *Channel) get() []*youtube.Channel { - service := auth.NewY2BService(youtube.YoutubeReadonlyScope) call := service.Channels.List(part) switch { case c.id != "": diff --git a/pkg/yutuber/playlist.go b/pkg/yutuber/playlist.go index 422cbf3..fd94b5f 100644 --- a/pkg/yutuber/playlist.go +++ b/pkg/yutuber/playlist.go @@ -6,7 +6,6 @@ import ( "log" "strings" - "github.com/eat-pray-ai/yutu/pkg/auth" "google.golang.org/api/youtube/v3" ) @@ -68,7 +67,6 @@ func (p *Playlist) Insert() { }, } - service := auth.NewY2BService(youtube.YoutubeScope) call := service.Playlists.Insert([]string{"snippet", "status"}, upload) playlist, err := call.Do() if err != nil { @@ -79,7 +77,6 @@ func (p *Playlist) Insert() { } func (p *Playlist) get(parts []string) []*youtube.Playlist { - service := auth.NewY2BService(youtube.YoutubeReadonlyScope) call := service.Playlists.List(parts) switch { case p.id != "": diff --git a/pkg/yutuber/video.go b/pkg/yutuber/video.go index adba024..57433da 100644 --- a/pkg/yutuber/video.go +++ b/pkg/yutuber/video.go @@ -7,7 +7,6 @@ import ( "os" "strings" - "github.com/eat-pray-ai/yutu/pkg/auth" "google.golang.org/api/youtube/v3" ) @@ -57,7 +56,6 @@ func NewVideo(opts ...VideoOption) *Video { } func (v *Video) get(parts []string) []*youtube.Video { - service := auth.NewY2BService(youtube.YoutubeReadonlyScope) call := service.Videos.List(parts) if v.id != "" { call = call.Id(v.id) @@ -118,7 +116,6 @@ func (v *Video) Insert() { }, } - service := auth.NewY2BService(youtube.YoutubeUploadScope) call := service.Videos.Insert([]string{"agegating,snippet,status"}, upload) video, err := call.Media(file).Do() @@ -156,7 +153,6 @@ func (v *Video) Update() { } video.Status.Embeddable = v.embeddable - service := auth.NewY2BService(youtube.YoutubeScope) call := service.Videos.Update([]string{"snippet,status"}, video) _, err := call.Do() if err != nil {