diff --git a/auth.go b/auth.go index a298091..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 ) @@ -34,7 +32,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) } @@ -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/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()) 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= 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