From 7b44386ccaa609e8deb1541d0bdf2d17f67e856c Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Sat, 22 Jul 2023 00:11:10 +0900 Subject: [PATCH 1/6] change function name from setTokenInfo to saveToken (#16) --- auth.go | 2 +- env.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/auth.go b/auth.go index a298091..767e07f 100644 --- a/auth.go +++ b/auth.go @@ -34,7 +34,7 @@ func completeAuth(w http.ResponseWriter, r *http.Request) { } // set token config - if err := setTokenInfo(token); err != nil { + if err := saveToken(token); err != nil { log.Fatal(err) } diff --git a/env.go b/env.go index 4e5edb0..691d523 100644 --- a/env.go +++ b/env.go @@ -86,7 +86,7 @@ func initEnv() error { return nil } -func setTokenInfo(token *oauth2.Token) error { +func saveToken(token *oauth2.Token) error { tokenViper.Set(AccessTokenKeyName, token.AccessToken) tokenViper.Set(RefreshTokenKeyName, token.RefreshToken) tokenViper.Set(ExpirationKeyName, token.Expiry.Unix()) From 7492de0a45dbf019dfa7c744e67d153077b9ef8d Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Sat, 22 Jul 2023 00:12:27 +0900 Subject: [PATCH 2/6] update go.mod (#16) --- go.mod | 3 ++- go.sum | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index bf66bab..4431e1d 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,11 @@ go 1.19 require ( github.com/google/uuid v1.3.0 github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 + github.com/sashabaranov/go-openai v1.14.0 github.com/spf13/cobra v1.7.0 github.com/spf13/viper v1.16.0 github.com/zmb3/spotify/v2 v2.3.1 + golang.org/x/oauth2 v0.7.0 ) require ( @@ -24,7 +26,6 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.4.2 // indirect golang.org/x/net v0.10.0 // indirect - golang.org/x/oauth2 v0.7.0 // indirect golang.org/x/sys v0.8.0 // indirect golang.org/x/text v0.9.0 // indirect google.golang.org/appengine v1.6.7 // indirect diff --git a/go.sum b/go.sum index 3eba5e1..2725df9 100644 --- a/go.sum +++ b/go.sum @@ -157,6 +157,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1: github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sashabaranov/go-openai v1.14.0 h1:D1yAB+DHElgbJFdYyjxfTWMFzhddn+PwZmkQ039L7mQ= +github.com/sashabaranov/go-openai v1.14.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM= github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA= From 6c546b11abced92842317943a665929e2b6397d2 Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Sat, 22 Jul 2023 00:13:06 +0900 Subject: [PATCH 3/6] get new access tokens by using new refresh tokens (#16) --- auth.go | 8 -------- main.go | 26 ++++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/auth.go b/auth.go index 767e07f..c1eb5cd 100644 --- a/auth.go +++ b/auth.go @@ -9,7 +9,6 @@ import ( "github.com/google/uuid" "github.com/pkg/browser" "github.com/zmb3/spotify/v2" - spotifyauth "github.com/zmb3/spotify/v2/auth" ) const ( @@ -17,7 +16,6 @@ const ( ) var ( - auth *spotifyauth.Authenticator state string server *http.Server ) @@ -50,12 +48,6 @@ func completeAuth(w http.ResponseWriter, r *http.Request) { } func authorize() { - auth = spotifyauth.New( - spotifyauth.WithClientID(clientViper.GetString(SpotifyIDKeyName)), - spotifyauth.WithClientSecret(clientViper.GetString(SpotifySecretKeyName)), - spotifyauth.WithRedirectURL(redirectURI), - spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate), - ) server = &http.Server{Addr: fmt.Sprintf(":%s", port)} state = uuid.New().String() http.HandleFunc("/callback", completeAuth) diff --git a/main.go b/main.go index a1adf7c..11f96ea 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ const ( ) var ( + auth *spotifyauth.Authenticator clientChannel = make(chan *spotify.Client, 1) ) @@ -27,6 +28,13 @@ func main() { log.Fatal(err) } + auth = spotifyauth.New( + spotifyauth.WithClientID(clientViper.GetString(SpotifyIDKeyName)), + spotifyauth.WithClientSecret(clientViper.GetString(SpotifySecretKeyName)), + spotifyauth.WithRedirectURL(redirectURI), + spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate), + ) + // check if token viper is set if !isClientInfoSet() { if err := askClientInfo(); err != nil { @@ -37,8 +45,22 @@ func main() { } else { ctx := context.Background() token := getToken() - httpClient := spotifyauth.New().Client(ctx, token) - clientChannel <- spotify.New(httpClient) + + newToken, err := auth.RefreshToken(ctx, token) + if err != nil { + log.Fatalf("fail to get a new access token: %v", err) + } + + // update an access token if it has expired + if token.AccessToken != newToken.AccessToken { + if err := saveToken(newToken); err != nil { + log.Fatalf("fail to save token: %v", err) + } + } + + spotifyClient := spotify.New(auth.Client(ctx, newToken)) + + clientChannel <- spotifyClient } client := <-clientChannel From 1c1633d693b4fea909bca82d8417bee734fb7cc8 Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Sat, 22 Jul 2023 00:16:18 +0900 Subject: [PATCH 4/6] comment out (#16) --- main.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 11f96ea..b318b54 100644 --- a/main.go +++ b/main.go @@ -43,24 +43,24 @@ func main() { authorize() } else { - ctx := context.Background() - token := getToken() + // ctx := context.Background() + // token := getToken() - newToken, err := auth.RefreshToken(ctx, token) - if err != nil { - log.Fatalf("fail to get a new access token: %v", err) - } + // newToken, err := auth.RefreshToken(ctx, token) + // if err != nil { + // log.Fatalf("fail to get a new access token: %v", err) + // } - // update an access token if it has expired - if token.AccessToken != newToken.AccessToken { - if err := saveToken(newToken); err != nil { - log.Fatalf("fail to save token: %v", err) - } - } + // // update an access token if it has expired + // if token.AccessToken != newToken.AccessToken { + // if err := saveToken(newToken); err != nil { + // log.Fatalf("fail to save token: %v", err) + // } + // } - spotifyClient := spotify.New(auth.Client(ctx, newToken)) + // spotifyClient := spotify.New(auth.Client(ctx, newToken)) - clientChannel <- spotifyClient + // clientChannel <- spotifyClient } client := <-clientChannel From 52f07f3cf3c1e7eda19160ad6710f29eb2e4786b Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Sat, 22 Jul 2023 00:21:25 +0900 Subject: [PATCH 5/6] comment out to pass linter (#16) --- env.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/env.go b/env.go index 691d523..54488b1 100644 --- a/env.go +++ b/env.go @@ -6,8 +6,6 @@ import ( "fmt" "os" "path/filepath" - "strconv" - "time" "github.com/spf13/viper" "golang.org/x/oauth2" @@ -98,20 +96,20 @@ func saveToken(token *oauth2.Token) error { return nil } -func getToken() *oauth2.Token { - accessToken := tokenViper.GetString(AccessTokenKeyName) - refreshToken := tokenViper.GetString(RefreshTokenKeyName) - expiration := tokenViper.GetString(ExpirationKeyName) - unix, _ := strconv.ParseInt(expiration, 10, 64) - expiry := time.Unix(unix, 0) - - return &oauth2.Token{ - AccessToken: accessToken, - TokenType: "bearer", - RefreshToken: refreshToken, - Expiry: expiry, - } -} +// func getToken() *oauth2.Token { +// accessToken := tokenViper.GetString(AccessTokenKeyName) +// refreshToken := tokenViper.GetString(RefreshTokenKeyName) +// expiration := tokenViper.GetString(ExpirationKeyName) +// unix, _ := strconv.ParseInt(expiration, 10, 64) +// expiry := time.Unix(unix, 0) + +// return &oauth2.Token{ +// AccessToken: accessToken, +// TokenType: "bearer", +// RefreshToken: refreshToken, +// Expiry: expiry, +// } +// } func askClientInfo() error { // get spotify ID From fd8f9c8fe3e9673d3cd82deaa66aff47b6d1edf9 Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Sat, 22 Jul 2023 00:23:55 +0900 Subject: [PATCH 6/6] ignore litner --- env.go | 30 ++++++++++++++++-------------- main.go | 28 ++++++++++++++-------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/env.go b/env.go index 54488b1..691d523 100644 --- a/env.go +++ b/env.go @@ -6,6 +6,8 @@ import ( "fmt" "os" "path/filepath" + "strconv" + "time" "github.com/spf13/viper" "golang.org/x/oauth2" @@ -96,20 +98,20 @@ func saveToken(token *oauth2.Token) error { return nil } -// func getToken() *oauth2.Token { -// accessToken := tokenViper.GetString(AccessTokenKeyName) -// refreshToken := tokenViper.GetString(RefreshTokenKeyName) -// expiration := tokenViper.GetString(ExpirationKeyName) -// unix, _ := strconv.ParseInt(expiration, 10, 64) -// expiry := time.Unix(unix, 0) - -// return &oauth2.Token{ -// AccessToken: accessToken, -// TokenType: "bearer", -// RefreshToken: refreshToken, -// Expiry: expiry, -// } -// } +func getToken() *oauth2.Token { + accessToken := tokenViper.GetString(AccessTokenKeyName) + refreshToken := tokenViper.GetString(RefreshTokenKeyName) + expiration := tokenViper.GetString(ExpirationKeyName) + unix, _ := strconv.ParseInt(expiration, 10, 64) + expiry := time.Unix(unix, 0) + + return &oauth2.Token{ + AccessToken: accessToken, + TokenType: "bearer", + RefreshToken: refreshToken, + Expiry: expiry, + } +} func askClientInfo() error { // get spotify ID diff --git a/main.go b/main.go index b318b54..11f96ea 100644 --- a/main.go +++ b/main.go @@ -43,24 +43,24 @@ func main() { authorize() } else { - // ctx := context.Background() - // token := getToken() + ctx := context.Background() + token := getToken() - // newToken, err := auth.RefreshToken(ctx, token) - // if err != nil { - // log.Fatalf("fail to get a new access token: %v", err) - // } + newToken, err := auth.RefreshToken(ctx, token) + if err != nil { + log.Fatalf("fail to get a new access token: %v", err) + } - // // update an access token if it has expired - // if token.AccessToken != newToken.AccessToken { - // if err := saveToken(newToken); err != nil { - // log.Fatalf("fail to save token: %v", err) - // } - // } + // update an access token if it has expired + if token.AccessToken != newToken.AccessToken { + if err := saveToken(newToken); err != nil { + log.Fatalf("fail to save token: %v", err) + } + } - // spotifyClient := spotify.New(auth.Client(ctx, newToken)) + spotifyClient := spotify.New(auth.Client(ctx, newToken)) - // clientChannel <- spotifyClient + clientChannel <- spotifyClient } client := <-clientChannel