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

add RefreshToken method for Authenticator #233

Merged
merged 2 commits into from
Sep 11, 2023

Conversation

JunNishimura
Copy link
Contributor

@JunNishimura JunNishimura commented Jul 21, 2023

Overview

Existing code did not have a mechanism to update access tokens with refresh tokens.
So I added RefreshToken method which returns a new access token if it has expired.

Example of use

ctx := context.Background()
token := getToken() // get an existing token somehow
auth := spotifyauth.New()
newToken, err := auth.RefreshToken(ctx, token) // refresh token here
if err != nil {
  fmt.Println(err)
}
if token.AccessToken != newToken.AccessToken {
  saveToken(newToken) // save a new token
}
client := spotify.New(auth.Client(ctx, newToken))

Related

@JunNishimura
Copy link
Contributor Author

@strideynet
I added RefreshToken method.
I would appreciate it if you check this PR!

@Padi142
Copy link

Padi142 commented Sep 7, 2023

I would love to see this merged. I get a lot of problems with my tokens not refreshing

@JunNishimura
Copy link
Contributor Author

@Padi142
Unfortunately, this repo has been inactive recently.

If you need the refresh token functionality right now, you can import my forked repo in which I merged this PR.
https://github.com/JunNishimura/spotify

@@ -171,6 +171,13 @@ func (a Authenticator) Token(ctx context.Context, state string, r *http.Request,
return a.config.Exchange(ctx, code, opts...)
}

// Return a new token if an access token has expired.
// If it has not expired, return the existing token.
func (a Authenticator) RefreshToken(ctx context.Context, token *oauth2.Token) (*oauth2.Token, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure calling this RefreshToken aligns with the GoDoc comment. Perhaps GetToken would be a clearer name - since the refreshing only occurs in certain circumstances.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@strideynet
Thank you for you opinion, and I understand what you are saying.

One concern I have with naming it GetToken is that new users of this library may mistakenly think that they can get *oauth2.Token just by calling the GetToken function. (In fact, users need to call Authenticator Token method to get the token for the first time.)

What do you think about this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you are saying - and agree. Let's move forwards with this.

@@ -171,6 +171,13 @@ func (a Authenticator) Token(ctx context.Context, state string, r *http.Request,
return a.config.Exchange(ctx, code, opts...)
}

// Return a new token if an access token has expired.
// If it has not expired, return the existing token.
func (a Authenticator) RefreshToken(ctx context.Context, token *oauth2.Token) (*oauth2.Token, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you are saying - and agree. Let's move forwards with this.

@strideynet strideynet merged commit 7154272 into zmb3:master Sep 11, 2023
@Padi142
Copy link

Padi142 commented Sep 11, 2023

<3 <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants