From ce5f0b223ea0ec74d7ea3b04d39fac370b1d64f6 Mon Sep 17 00:00:00 2001 From: chaoqun <27287694+OpenWaygate@users.noreply.github.com> Date: Sun, 21 Apr 2024 15:30:46 +0800 Subject: [PATCH] :children_crossing: Persist refreshed token --- README.md | 6 +++--- pkg/auth/auth.go | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d8a2c98..a12899e 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Here are the features that are currently supported by yutu, and the ones that ar - [x] update, 50 - [ ] delete, 50 - activities - - [ ] list, 1 + - [x] list, 1 - captions - [ ] list, 50 - [ ] insert, 400 @@ -87,8 +87,8 @@ Here are the features that are currently supported by yutu, and the ones that ar - [ ] list, 1 - [ ] insert, 50 - [ ] update, 50 -- guideCategories - - [ ] list, 1 +- guideCategories + - [x] list, 1 deprecated API - i18nLanguages - [ ] list, 1 - i18nRegions diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index ca3aef8..58be4f5 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -21,7 +21,7 @@ import ( var ( credential string = "client_secret.json" - cacheFile string = "youtube.cache.json" + cacheFile string = "youtube.token.json" errGetUser error = errors.New("unable to get current user") errCreateSvc error = errors.New("unable to create YouTube service") errReadPrompt error = errors.New("unable to read prompt") @@ -68,6 +68,9 @@ func getClient(ctx context.Context, scope ...string) *http.Client { } else if !token.Valid() { tokenSource := config.TokenSource(ctx, token) token, err = tokenSource.Token() + if token != nil { + saveToken(cacheFile, token) + } if err != nil { return newClient(ctx, config, cacheFile) } @@ -208,7 +211,6 @@ func tokenFromFile(file string) (*oauth2.Token, error) { } func saveToken(file string, token *oauth2.Token) { - fmt.Printf("Saving credential file to %s\n", file) f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { log.Fatalln(errors.Join(errCacheToken, err))