Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get new access tokens with refresh tokens #17

Merged
merged 6 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import (
"github.com/google/uuid"
"github.com/pkg/browser"
"github.com/zmb3/spotify/v2"
spotifyauth "github.com/zmb3/spotify/v2/auth"
)

const (
port = "8888"
)

var (
auth *spotifyauth.Authenticator
state string
server *http.Server
)
Expand All @@ -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)
}

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
26 changes: 24 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
)

var (
auth *spotifyauth.Authenticator
clientChannel = make(chan *spotify.Client, 1)
)

Expand All @@ -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 {
Expand All @@ -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
Expand Down