[GH-ISSUE #72] Unsure on how to use the refresh token #38

Closed
opened 2026-02-27 19:25:44 +03:00 by kerem · 8 comments
Owner

Originally created by @Cannonb4ll on GitHub (Mar 23, 2017).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/72

Currently creating an app where users basically login, and after they login they can hookup their spotify account to their account in the app im creating.

I get a accessToken and refreshToken, but what is the structural way to use this? I now have after a few minutes/hours that the accesstoken gets expired (which I save in my database).

How do I extend the accestoken with the refreshtoken, and when?

Originally created by @Cannonb4ll on GitHub (Mar 23, 2017). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/72 Currently creating an app where users basically login, and after they login they can hookup their spotify account to their account in the app im creating. I get a accessToken and refreshToken, but what is the structural way to use this? I now have after a few minutes/hours that the accesstoken gets expired (which I save in my database). How do I extend the accestoken with the refreshtoken, and when?
kerem 2026-02-27 19:25:44 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Mar 24, 2017):

When the access token expires (either by you catching the error from Spotify or checking it against your database) you use the Session::refreshAccessToken() to request new tokens, which you then save in the database.

There's some more info and examples here: https://jwilsson.github.io/spotify-web-api-php/authorization.html#step-2

<!-- gh-comment-id:288960480 --> @jwilsson commented on GitHub (Mar 24, 2017): When the access token expires (either by you catching the error from Spotify or checking it against your database) you use the `Session::refreshAccessToken()` to request new tokens, which you then save in the database. There's some more info and examples here: https://jwilsson.github.io/spotify-web-api-php/authorization.html#step-2
Author
Owner

@Cannonb4ll commented on GitHub (Mar 24, 2017):

Thank you. Would it be bad practise to execute that method each time the user logs in?

<!-- gh-comment-id:288960670 --> @Cannonb4ll commented on GitHub (Mar 24, 2017): Thank you. Would it be bad practise to execute that method each time the user logs in?
Author
Owner

@jwilsson commented on GitHub (Mar 24, 2017):

Hmm, I think the best way is to only request it when needed. I don't know if Spotify might start blocking your app or something if you request tokens too often.

<!-- gh-comment-id:288962789 --> @jwilsson commented on GitHub (Mar 24, 2017): Hmm, I think the best way is to only request it when needed. I don't know if Spotify might start blocking your app or something if you request tokens too often.
Author
Owner

@Cannonb4ll commented on GitHub (Mar 24, 2017):

Alright, with a try-catch?

<!-- gh-comment-id:288963415 --> @Cannonb4ll commented on GitHub (Mar 24, 2017): Alright, with a try-catch?
Author
Owner

@jwilsson commented on GitHub (Mar 24, 2017):

Yes, exactly! And when it fails, you refresh the token (if it's that kind of error).

<!-- gh-comment-id:288977155 --> @jwilsson commented on GitHub (Mar 24, 2017): Yes, exactly! And when it fails, you refresh the token (if it's that kind of error).
Author
Owner

@Cannonb4ll commented on GitHub (Mar 24, 2017):

Alright I think I get that! So sorta like this:

            $api = new \SpotifyWebAPI\SpotifyWebAPI();

            try{
                $api->setAccessToken($token);

                $artists = object_get($api->getUserFollowedArtists(), 'artists.items');

            } catch (\Exception $e){
                $session = new \SpotifyWebAPI\Session('CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URI');
                $session->refreshAccessToken($refreshToken);

                $accessToken = $session->getAccessToken();

                $api->setAccessToken($accessToken);

                $artists = object_get($api->getUserFollowedArtists(), 'artists.items');
            }

    // continue doing my things here
<!-- gh-comment-id:288977521 --> @Cannonb4ll commented on GitHub (Mar 24, 2017): Alright I think I get that! So sorta like this: ``` $api = new \SpotifyWebAPI\SpotifyWebAPI(); try{ $api->setAccessToken($token); $artists = object_get($api->getUserFollowedArtists(), 'artists.items'); } catch (\Exception $e){ $session = new \SpotifyWebAPI\Session('CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URI'); $session->refreshAccessToken($refreshToken); $accessToken = $session->getAccessToken(); $api->setAccessToken($accessToken); $artists = object_get($api->getUserFollowedArtists(), 'artists.items'); } // continue doing my things here ```
Author
Owner

@jwilsson commented on GitHub (Mar 24, 2017):

Yeah, I would check the code and message of the Exception too. Just to see if it's an expired access token error. I don't know the exakt message to look for though, but the status code should be 401.

<!-- gh-comment-id:288985874 --> @jwilsson commented on GitHub (Mar 24, 2017): Yeah, I would check the code and message of the `Exception` too. Just to see if it's an expired access token error. I don't know the exakt message to look for though, but the status code should be `401`.
Author
Owner

@Cannonb4ll commented on GitHub (Mar 24, 2017):

Thank you for your help, I will do that. Solved! :)

<!-- gh-comment-id:288986298 --> @Cannonb4ll commented on GitHub (Mar 24, 2017): Thank you for your help, I will do that. Solved! :)
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#38
No description provided.