mirror of
https://github.com/jwilsson/spotify-web-api-php.git
synced 2026-04-26 23:45:49 +03:00
[GH-ISSUE #137] Automatic refresh of access token #84
Labels
No labels
bug
docs
enhancement
enhancement
enhancement
feedback wanted
good first issue
help wanted
help wanted
help wanted
invalid
pull-request
question
question
upstream
upstream
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/spotify-web-api-php#84
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @henk23 on GitHub (Aug 12, 2018).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/137
Hey, if I did not miss anything we have two options to notice that an access token refresh is needed:
To me this seems something that could be automated, because basically every developer using this library is going to need this at some point.
I thought of something like this:
I could try to make a PR if this would be desired functionality.
@jwilsson commented on GitHub (Aug 14, 2018):
Hi!
Thanks for the suggestion!
I don't know how feasible this would be with the current architecture, given how separated the
SessionandSpotifyWebAPIclasses are. They don't know anything about each other and since the user of the library is responsible for passing access token between them I don't know how this could be automated I'm afraid.You're obviously more than welcome to try something out but I can't promise it'll be included in the library. My only suggestion if you do try to do something would be to check the expiration time before each call and refresh the token as needed.
@henk23 commented on GitHub (Aug 23, 2018):
Hey. I tried and failed. ;)
I ended up writing a
SpotifyServiceto wrapSessionandSpotifyWebAPI, checking the expirationTime before returning a valid api instance. But since the storage of the accesToken, refreshToken and expirationTime depends on the particular project, I also don't see how to implement this in a general way to include in this repo.@adamazad commented on GitHub (Aug 23, 2018):
I wrote a code to swap token 10 minutes before they expire (PHP run via
Node). I have a project that let's you schedule adding tracks to playlist
and this required fresh tokens at any given time.
On Thu, Aug 23, 2018, 4:41 PM henk23 notifications@github.com wrote:
@SanderSander commented on GitHub (Aug 24, 2018):
It should be possible to detect that the access token is expired, and on that moment use the refresh token to obtain a new one.
From the docs:
Ofcourse it would be more efficient to check the
expires_in, instead for waiting for the expired status code. but checking theexpires_inisn't fail proof.But when the
SpotifyWebAPIhas a method likesetSession(Session $session)or by injecting it throug the constructor or even by extending theRequestclass toSessionBasedRequestit should be possible to auto refresh the access token. Even when a expire response is returned by spotify by handling that exception whensessionis set in the API.I can make a PR and try some stuff because I really need this.
@SanderSander commented on GitHub (Aug 24, 2018):
Ok after some testing this worked for me, it isn't really nice YET. but it works without changing any code in the repository.
This could be added without breaking anything, and with tests and fixing the ugly stuff (Like
setAPI) etc, so this feature is doable and requires a bit more effort.In the constructor of
SpotifyWebAPIit would be possible to do aninstanceof SessionBasedRequestand than add a methodgetSessionto the request objectAnd the bootstrap code from README
What do you guys think?
@doekenorg commented on GitHub (Oct 4, 2018):
I have implemented roughly the same solution in my app.
I think it would be a great asset if we'd have a specific exception for this situation. I'd like the library to check against the exception message and throw a
SpotifyWebApiExpiredTokenException.Still think that wrapping your requests within a try catch is a valid way of dealing. I just don't like the extra check on the message.
@jwilsson commented on GitHub (Oct 7, 2018):
@doekenorg I think adding a
SpotifyWebApiExpiredTokenExceptionwould open up the door for too many highly specific exceptions. To me, there's two "types" of exceptions, API ones (missing parameters, invalid track ID etc.) and Auth ones (invalid client, expired token etc.). I'm not that keen on adding anymore exceptions at this point.@doekenorg commented on GitHub (Oct 7, 2018):
@jwilsson fair enough!
But since this is an auth error related, are you willing to add helpers to the Auth Exception? Something along the lines of
hasInvalidCredentials()orhasExpiredToken()? These could be easy checks against the returned message. I just feel like checking against those messages in production code is like using$something === 'true'. It just doesn't feel right 😉, and it could break down easy when spotify decides to change it's exceptions messages. One update of this package would save everybody 😄 .@jwilsson commented on GitHub (Oct 9, 2018):
@doekenorg Sure, sounds like it could be helpful! Wanna send a PR for it?
@doekenorg commented on GitHub (Oct 9, 2018):
Sure. Will do
@lewislarsen commented on GitHub (Sep 1, 2019):
Any update on this?
@bulgariamitko commented on GitHub (Sep 1, 2019):
my simple solution:
try { $api->me(); } catch (Exception $e) { if ($e->getMessage() == 'The access token expired') { // Fetch the refresh token from somewhere. A database for example. $session->refreshAccessToken($refreshToken); $accessToken = $session->getAccessToken(); // [Save this accessToken variable to the DB or Session // Set our new access token on the API wrapper and continue to use the API as usual $api->setAccessToken($accessToken); } echo '<pre>'; print_r($e->getMessage()); echo '</pre>'; echo '<pre>'; print_r('Refresh Token'); echo '</pre>'; }@jwilsson commented on GitHub (Sep 28, 2019):
Hi everyone!
So after playing around with some of @SanderSander's ideas in https://github.com/jwilsson/spotify-web-api-php/issues/137#issuecomment-415789512 I've reached a solution that I'm pretty satisfied with and which I hope will solve everyone's needs.
I've pushed it to it's own branch and created a PR for it for anyone interested to try out (install it with
composer require jwilsson/spotify-web-api-php:dev-auto-refresh). It's still missing tests and some more docs (only the basics to get you up and running are there atm).But please try it out and let me know of any issues, thoughts, and/or ideas!
@jwilsson commented on GitHub (Oct 19, 2019):
This has been released in
2.11.0! 🎉Docs are available here.