jwilsson / spotify-web-api-php

A PHP wrapper for Spotify's Web API.
MIT License
867 stars 156 forks source link

Auto refresh option without refreshToken #262

Closed upgrader-dev closed 1 year ago

upgrader-dev commented 1 year ago

Hello,

First of all thank you for your work:)

Initially I was not using the auto_refresh option provided by your code.

I'd like to use it but I can't understand how it could work without providing the refreshToken.

See SpotifyWebAPI.php line122 :

catch (SpotifyWebAPIException $e) {
            if ($this->options['auto_refresh'] && $e->hasExpiredToken()) {
                $refreshToken = $this->session->**getRefreshToken();**
                $result = $this->session->refreshAccessToken();

Maybe a solution would be to retrieve the refreshToken from the session object property or in argument of the function.

jwilsson commented 1 year ago

Hey! Thank you 😊

I don't know if you've seen the docs on how to set it auto refresh but basically what you need to do is to create a Session instance and provide a refresh token, and then pass that Session instance to SpotifyWebAPI.

What then happens "behind the scenes" is:

  1. In SpotifyWebAPI::sendRequest() (which you linked above), $session->refreshAccessToken() requests a new access token given the refresh token you passed it earlier and sets the new tokens on itself.
  2. SpotifyWebAPI::sendRequest() continues on to call itself with the same arguments, causing SpotifyWebAPI::authHeaders() to be called which will grab the new access token from its Session instance and set that in the request headers.
  3. The original request will then be sent again but with a new access token, successfully completing that call.

Hope this clarifies things 😄

upgrader-dev commented 1 year ago

Hello,

Sorry I hadn’t find the docs about auto refresh, and I’m still a beginner :)

What you describe is what I was doing without success. I was getting an « no Token provided » error.

But finally I had found that passing the session object when making the instance of Api was a better way.

Thank you very much for this feature, it allows me to manage the entire Api in one service and only getting the api object on each page like magic :)

I close the issue

Regards

Benjamin Ghedini

Le 3 juin 2023 à 08:59, Jonathan Wilsson @.***> a écrit :

Hey! Thank you 😊

I don't know if you've seen the docs on how to set it auto refresh https://github.com/jwilsson/spotify-web-api-php/blob/ecbe3e676f287675aef733e205a440c85658d765/docs/examples/refreshing-access-tokens.md#automatically-refreshing-access-tokens but basically what you need to do is to create a Session instance and provide a refresh token, then pass that Session instance to SpotifyWebAPI.

What then happens "behind the scenes" is:

In SpotifyWebAPI::sendRequest (which you linked above), $session->refreshAccessToken() requests a new access token given the refresh token you passed it earlier and sets the new tokens on itself. SpotifyWebAPI::sendRequest continues on to call itself with the same arguments, causing SpotifyWebAPI::authHeaders() to be called which will grab the new access token from its Session instance. The original request will then be sent again but with a new access token, successfully completing that call. Hope this clarifies things 😄

— Reply to this email directly, view it on GitHub https://github.com/jwilsson/spotify-web-api-php/issues/262#issuecomment-1574730485, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2YPLL7UVKBC7VLL6AXUM2DXJLOGBANCNFSM6AAAAAAYYWWKTY. You are receiving this because you authored the thread.