[GH-ISSUE #262] Auto refresh option without refreshToken #192

Closed
opened 2026-02-27 19:26:31 +03:00 by kerem · 2 comments
Owner

Originally created by @upgrader-dev on GitHub (Jun 2, 2023).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/262

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.

Originally created by @upgrader-dev on GitHub (Jun 2, 2023). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/262 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.
kerem 2026-02-27 19:26:31 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Jun 3, 2023):

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 😄

<!-- gh-comment-id:1574730485 --> @jwilsson commented on GitHub (Jun 3, 2023): 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, 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 😄
Author
Owner

@upgrader-dev commented on GitHub (Jun 3, 2023):

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.

<!-- gh-comment-id:1574762579 --> @upgrader-dev commented on GitHub (Jun 3, 2023): 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. >
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#192
No description provided.