zmb3 / spotify

A Go wrapper for the Spotify Web API
Apache License 2.0
1.33k stars 284 forks source link

Add helper method for extracting current token #169

Closed strideynet closed 2 years ago

strideynet commented 2 years ago

Developers may wish to capture the currently active access/refresh tokens, rather than persisting the original tokens after the oauth2 flow completes.

Assists #168

elivlo commented 2 years ago

I think it would be a great idea adding again this function in v2:

// Token gets the client's current token.
func (c *Client) Token() (*oauth2.Token, error) {
    transport, ok := c.http.Transport.(*oauth2.Transport)
    if !ok {
        return nil, errors.New("spotify: oauth2 transport type not correct")
    }
    t, err := transport.Source.Token()
    if err != nil {
        return nil, err
    }

    return t, nil
}

The main reason behind this proposal is that, when you create a new spotify.Client with OAuth credentials that are expired, you will receive a new token. But because this newly returned token is stored inside spotify.Client.http, you have no access to the object variable from outside and are not able to store it in case the app exits.

elivlo commented 2 years ago

Hi @strideynet, I just saw that this Issue is still open but was resolved by PR #170 :)

strideynet commented 2 years ago

Thanks Elivlo