[GH-ISSUE #51] Access token is always expired #28

Closed
opened 2026-02-27 19:25:41 +03:00 by kerem · 1 comment
Owner

Originally created by @kobelobster on GitHub (Aug 21, 2016).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/51

Hey,

using your api to retrieve user's playlists and do some more stuff. When I call a method to the API I call my own method refreshTokens. This method checks if the token expiraton is less than 60 seconds and then updates the refresh tokens. However, I'm always running into the case true for $this->session->getTokenExpiration() - time() and I don't know why. It seems like the access token gets refreshed but as soon as I refresh my page it's empty again?

This is my constructor.

public function __construct(
    Session $session,
    SpotifyWebAPI $spotifyWebAPI,
    Guard $guard,
    UserRepository $userRepository
) {
    $this->session = $session;
    $this->spotifyWebAPI = $spotifyWebAPI;
    $this->guard = $guard;

    if ($this->session->getAccessToken() !== '') {
        $this->spotifyWebAPI->setAccessToken($this->session->getAccessToken());
    }

    $this->userRepository = $userRepository;
}

And this is the method I always call inside my methods calling the API.

/**
 * Refreshes the token if the expiration is under the threshold or the force option is set to true.
 *
 * @param int  $threshold
 * @param bool $force
 */
public function refreshTokens(int $threshold = 60, bool $force = false)
{
    var_dump($this->session->getTokenExpiration()); // 0
    if ($this->session->getTokenExpiration() - time() < $threshold || $force) {
        $refreshToken = $this->getRefreshToken();
        var_dump($this->session->refreshAccessToken($refreshToken)); // true
        print $this->session->getTokenExpiration() - time(); // 3600
        $this->spotifyWebAPI->setAccessToken($this->session->getAccessToken());
        $this->updateTokensToUser();
    }
}

What am I doing wrong here?

Originally created by @kobelobster on GitHub (Aug 21, 2016). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/51 Hey, using your api to retrieve user's playlists and do some more stuff. When I call a method to the API I call my own method `refreshTokens`. This method checks if the token expiraton is less than 60 seconds and then updates the refresh tokens. However, I'm always running into the case true for `$this->session->getTokenExpiration() - time()` and I don't know why. It seems like the access token gets refreshed but as soon as I refresh my page it's empty again? This is my constructor. ``` public function __construct( Session $session, SpotifyWebAPI $spotifyWebAPI, Guard $guard, UserRepository $userRepository ) { $this->session = $session; $this->spotifyWebAPI = $spotifyWebAPI; $this->guard = $guard; if ($this->session->getAccessToken() !== '') { $this->spotifyWebAPI->setAccessToken($this->session->getAccessToken()); } $this->userRepository = $userRepository; } ``` And this is the method I always call inside my methods calling the API. ``` /** * Refreshes the token if the expiration is under the threshold or the force option is set to true. * * @param int $threshold * @param bool $force */ public function refreshTokens(int $threshold = 60, bool $force = false) { var_dump($this->session->getTokenExpiration()); // 0 if ($this->session->getTokenExpiration() - time() < $threshold || $force) { $refreshToken = $this->getRefreshToken(); var_dump($this->session->refreshAccessToken($refreshToken)); // true print $this->session->getTokenExpiration() - time(); // 3600 $this->spotifyWebAPI->setAccessToken($this->session->getAccessToken()); $this->updateTokensToUser(); } } ``` What am I doing wrong here?
kerem 2026-02-27 19:25:41 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Aug 29, 2016):

Hi,
Sorry for the late reply.

The library doesn't actually remember the time between page reloads, it just sets it whenever you request or refresh a token. If you save it yourself somewhere between page reloads and read the value from there it should work correctly.

<!-- gh-comment-id:243063049 --> @jwilsson commented on GitHub (Aug 29, 2016): Hi, Sorry for the late reply. The library doesn't actually remember the time between page reloads, it just sets it whenever you request or refresh a token. If you save it yourself somewhere between page reloads and read the value from there it should work correctly.
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#28
No description provided.