mirror of
https://github.com/jwilsson/spotify-web-api-php.git
synced 2026-04-26 23:45:49 +03:00
[GH-ISSUE #52] Fatal error: Uncaught exception on page refresh #31
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#31
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 @ghost on GitHub (Sep 13, 2016).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/52
Hi, I am getting the following error when I refresh my Spotify web app...
Fatal error: Uncaught exception 'SpotifyWebAPI\SpotifyWebAPIException' with message 'Invalid authorization code' in /Users/user/Desktop/WEB-DEV/spotify-web-api-php-master/src/Request.php:170 Stack trace: #0 /Users/user/Desktop/WEB-DEV/spotify-web-api-php-master/src/Request.php(50): SpotifyWebAPI\Request->send('POST', 'https://account...', Array, Array) #1 /Users/user/Desktop/WEB-DEV/spotify-web-api-php-master/src/Session.php(205): SpotifyWebAPI\Request->account('POST', '/api/token', Array, Array) #2 /Users/user/Desktop/WEB-DEV/spotify-web-api-php-master/includes/api-key.php(4): SpotifyWebAPI\Session->requestAccessToken('AQAaKVCyJ2eX1vE...') #3 /Users/user/Desktop/WEB-DEV/spotify-web-api-php-master/index.php(20): include('/Users/user/Des...') #4 {main} thrown in /Users/user/Desktop/WEB-DEV/spotify-web-api-php-master/src/Request.php on line 170
I think the reason for this error is because the URL has the code from Spotify as a query string on the end. If I delete everything from ?code onwards and reload the page it works fine. I just cant figure out how to not display the query string on the URL but keep the app working.
My code is as follows...
Thanks
@jwilsson commented on GitHub (Sep 14, 2016):
Hi!
I think the easiest way is to have two pages.
One page (let's call it
index.php) that contains your application and checks if an access token already exists (saved in a database, a session, or somewhere persistent). If an access token doesn't exist, send the user to the authorization URL with a redirect URI calledcallback.php.In
callback.phpyou exchange the$_GET['code']for an access token, save it somewhere, and redirect the user back toindex.php.@ghost commented on GitHub (Sep 14, 2016):
Awesome, thank you for your help! I've got it working, I think. Storing the $accessToken within a session variable and recalling it on index.php worked.
@dayeggpi commented on GitHub (Mar 2, 2017):
Hi !
I'm sorry, I'm not sure I understand this page : https://jwilsson.github.io/spotify-web-api-php/authorization.html
and I get the same error.
Here is my code, would appreciate some help :)
in ./index.php :
then in ./callback/index.php :
What happens is that if I go to https://mysite.com/ I immediately get redirected to https://mysite.com/callback?code=XXXXXXX and the result that I want is displayed. However if I refresh, I get this uncaught exception.
Could you please help me to understand your authorization doc page ?
thanks !
@jwilsson commented on GitHub (Mar 2, 2017):
@hycday Instead of printing the history in
./callback/index.php, save the token somewhere (PHP session, database, etc.) and redirect the user back toindex.php.In
index.php, check if you got a token. If there is one, use it and print the history. If not, send the user to the Spotify authorization page.Sloppy, untested example using PHP sessions: https://gist.github.com/jwilsson/07f30721b5a3378c78bf4c86cdc92b98
I can agree that the docs could be more clear, though. I'll look at updating it.
@dayeggpi commented on GitHub (Mar 2, 2017):
thank you @jwilsson
it's more clear now !
@dayeggpi commented on GitHub (Mar 3, 2017):
just out of curiosity, I managed to do it in PHP session (then each user will see their own tracks), but when I do it with a database, I am facing a problematic : they all see my tracks.
Indeed, in the database, (in which I have a token field, a timestamp field, and a userid field), I need to insert a row with a token (or update it for same userid, if timestamp is expired and userid exists), but I just used a fake userid manually inputed, so therefore, their is only one user, and hence they all see my tracks.
Is there a function to identify the user via some sort of ID, proper to Spotify ? or basically the database approach is not applicable ?
If I remove the userid field, how will I know which token to retrieve from the database, for each user...
Do you see what I mean ?
@jwilsson commented on GitHub (Mar 5, 2017):
@hycday Let's not hijack this issue anymore, drop me a line at
jonathan.wilsson at gmailand we can discuss further there.