[GH-ISSUE #29] Invalid authorization code #13

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

Originally created by @matthijsotterloo on GitHub (Mar 29, 2015).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/29

Hi

I'm having a problem....
https://beta.cloudmoosic.com/music/spotify/login
After login I'm getting the Invalid Authorization Code Error, I've used the examples you gave but it's still not working....

Any idea on how to fix this?

Originally created by @matthijsotterloo on GitHub (Mar 29, 2015). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/29 Hi I'm having a problem.... https://beta.cloudmoosic.com/music/spotify/login After login I'm getting the Invalid Authorization Code Error, I've used the examples you gave but it's still not working.... Any idea on how to fix this?
kerem closed this issue 2026-02-27 19:25:37 +03:00
Author
Owner

@hvt commented on GitHub (Mar 29, 2015):

What example are you using exactly?

<!-- gh-comment-id:87395370 --> @hvt commented on GitHub (Mar 29, 2015): What example are you using exactly?
Author
Owner

@matthijsotterloo commented on GitHub (Mar 29, 2015):

This is my code:

https://gist.github.com/matthijsotterloo/da9da7a90214cfbf9ab0

<!-- gh-comment-id:87395862 --> @matthijsotterloo commented on GitHub (Mar 29, 2015): This is my code: https://gist.github.com/matthijsotterloo/da9da7a90214cfbf9ab0
Author
Owner

@jwilsson commented on GitHub (Mar 29, 2015):

Hi,
Everything works fine for me when I test your code locally.

It looks like you're using CodeIgniter, could the framework interfere with the $_GET parameter?

<!-- gh-comment-id:87399707 --> @jwilsson commented on GitHub (Mar 29, 2015): Hi, Everything works fine for me when I test your code locally. It looks like you're using CodeIgniter, could the framework interfere with the $_GET parameter?
Author
Owner

@matthijsotterloo commented on GitHub (Mar 29, 2015):

Ahh, thats the problem!
Any idea how I can fix it?

<!-- gh-comment-id:87404585 --> @matthijsotterloo commented on GitHub (Mar 29, 2015): Ahh, thats the problem! Any idea how I can fix it?
Author
Owner

@hvt commented on GitHub (Mar 29, 2015):

I don't know anything about CodeIgniter, but it appears you have to replace $_GET['code'] with $this->input->get('code').

<!-- gh-comment-id:87411068 --> @hvt commented on GitHub (Mar 29, 2015): I don't know anything about CodeIgniter, but it appears you have to replace `$_GET['code']` with `$this->input->get('code')`.
Author
Owner

@matthijsotterloo commented on GitHub (Mar 29, 2015):

Still the same error

<!-- gh-comment-id:87411180 --> @matthijsotterloo commented on GitHub (Mar 29, 2015): Still the same error
Author
Owner

@jwilsson commented on GitHub (Mar 29, 2015):

I'm not very familiar with CodeIgniter either, but the docs might be of some help: https://ellislab.com/codeigniter/user-guide/libraries/input.html

It looks like this might work:
$this->input->get('code', TRUE);

<!-- gh-comment-id:87421439 --> @jwilsson commented on GitHub (Mar 29, 2015): I'm not very familiar with CodeIgniter either, but the docs might be of some help: https://ellislab.com/codeigniter/user-guide/libraries/input.html It looks like this might work: `$this->input->get('code', TRUE);`
Author
Owner

@matthijsotterloo commented on GitHub (Mar 29, 2015):

Not working... Running in to a new error now,

Fatal error: Uncaught exception 'SpotifyWebAPI\SpotifyWebAPIException' with message 'Invalid client' in /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php:139 Stack trace: #0 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php(26): SpotifyWebAPI\Request->send('POST', 'https://account...', Array, Array) #1 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Session.php(229): SpotifyWebAPI\Request->account('POST', '/api/token', Array) #2 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/spotify.php(23): SpotifyWebAPI\Session->requestAccessToken('AQAakd0ztViyK4L...') #3 /home/cloudmoosic/www/beta.cloudmoosic.com/system/core/CodeIgniter.php(250): include('/home/cloudmoos...') #4 /home/cloudmoosic/www/beta.cloudmoosic.com/index.php(232): require_once('/home/cloudmoos...') #5 {main} thrown in /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php on line 139

<!-- gh-comment-id:87454841 --> @matthijsotterloo commented on GitHub (Mar 29, 2015): Not working... Running in to a new error now, Fatal error: Uncaught exception 'SpotifyWebAPI\SpotifyWebAPIException' with message 'Invalid client' in /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php:139 Stack trace: #0 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php(26): SpotifyWebAPI\Request->send('POST', 'https://account...', Array, Array) #1 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Session.php(229): SpotifyWebAPI\Request->account('POST', '/api/token', Array) #2 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/spotify.php(23): SpotifyWebAPI\Session->requestAccessToken('AQAakd0ztViyK4L...') #3 /home/cloudmoosic/www/beta.cloudmoosic.com/system/core/CodeIgniter.php(250): include('/home/cloudmoos...') #4 /home/cloudmoosic/www/beta.cloudmoosic.com/index.php(232): require_once('/home/cloudmoos...') #5 {main} thrown in /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php on line 139
Author
Owner

@jwilsson commented on GitHub (Mar 29, 2015):

Looks like something's wrong with your app credentials, invalid client ID, wrong redirect URI etc.

<!-- gh-comment-id:87459437 --> @jwilsson commented on GitHub (Mar 29, 2015): Looks like something's wrong with your app credentials, invalid client ID, wrong redirect URI etc.
Author
Owner

@matthijsotterloo commented on GitHub (Mar 30, 2015):

Yea I see, any idea on how to integrate it with codeigniter? :(

<!-- gh-comment-id:87700991 --> @matthijsotterloo commented on GitHub (Mar 30, 2015): Yea I see, any idea on how to integrate it with codeigniter? :(
Author
Owner

@hvt commented on GitHub (Mar 30, 2015):

What's your current code?

-- Harm

On Mon, Mar 30, 2015 at 4:26 PM, Matthijs Otterloo <notifications@github.com

wrote:

Yea I see, any idea on how to integrate it with codeigniter? :(


Reply to this email directly or view it on GitHub
https://github.com/jwilsson/spotify-web-api-php/issues/29#issuecomment-87700991
.

<!-- gh-comment-id:87704797 --> @hvt commented on GitHub (Mar 30, 2015): What's your current code? -- Harm On Mon, Mar 30, 2015 at 4:26 PM, Matthijs Otterloo <notifications@github.com > wrote: > > Yea I see, any idea on how to integrate it with codeigniter? :( > > — > Reply to this email directly or view it on GitHub > https://github.com/jwilsson/spotify-web-api-php/issues/29#issuecomment-87700991 > .
Author
Owner

@matthijsotterloo commented on GitHub (Mar 30, 2015):

load->helper('url'); } public function login() { require_once 'vendor/Request.php'; require_once 'vendor/Session.php'; require_once 'vendor/SpotifyWebAPI.php'; require_once 'vendor/SpotifyWebAPIException.php'; $session = new SpotifyWebAPI\Session( '6428226f8930472080acde29db277e37', '73aec018a2724fe1985fc88130b44c86', 'https://beta.cloudmoosic.com/music/spotify/login' ); $scopes = array( 'user-read-email', 'user-library-modify' ); $api = new SpotifyWebAPI\SpotifyWebAPI(); // Redirect to Spotify login screen if user is not logged in. if (isset($_GET['code'])) { $session->requestAccessToken($_GET['code']); $accessToken = $session->getAccessToken(); // Set the access token on the API wrapper $api->setAccessToken($accessToken); // Request a access token using the code from Spotify print_r($api->me()); } else { $authorizeUrl = $session->getAuthorizeUrl(array( 'scope' => $scopes )); header('Location: ' . $authorizeUrl); die(); } } ``` }
<!-- gh-comment-id:87705621 --> @matthijsotterloo commented on GitHub (Mar 30, 2015): <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require_once 'vendor/Request.php'; require_once 'vendor/Session.php'; require_once 'vendor/SpotifyWebAPI.php'; require_once 'vendor/SpotifyWebAPIException.php'; class Spotify extends MY_Controller { ``` public function __construct() { parent::__construct(); // To use site_url and redirect on this controller. $this->load->helper('url'); } public function login() { require_once 'vendor/Request.php'; require_once 'vendor/Session.php'; require_once 'vendor/SpotifyWebAPI.php'; require_once 'vendor/SpotifyWebAPIException.php'; $session = new SpotifyWebAPI\Session( '6428226f8930472080acde29db277e37', '73aec018a2724fe1985fc88130b44c86', 'https://beta.cloudmoosic.com/music/spotify/login' ); $scopes = array( 'user-read-email', 'user-library-modify' ); $api = new SpotifyWebAPI\SpotifyWebAPI(); // Redirect to Spotify login screen if user is not logged in. if (isset($_GET['code'])) { $session->requestAccessToken($_GET['code']); $accessToken = $session->getAccessToken(); // Set the access token on the API wrapper $api->setAccessToken($accessToken); // Request a access token using the code from Spotify print_r($api->me()); } else { $authorizeUrl = $session->getAuthorizeUrl(array( 'scope' => $scopes )); header('Location: ' . $authorizeUrl); die(); } } ``` }
Author
Owner

@jwilsson commented on GitHub (Mar 30, 2015):

Try adding a trailing slash to the redirect URI, i.e. https://beta.cloudmoosic.com/music/spotify/login/. Both in your app's settings at https://developer.spotify.com/my-applications/ and in your code.

<!-- gh-comment-id:87715353 --> @jwilsson commented on GitHub (Mar 30, 2015): Try adding a trailing slash to the redirect URI, i.e. https://beta.cloudmoosic.com/music/spotify/login/. Both in your app's settings at https://developer.spotify.com/my-applications/ and in your code.
Author
Owner

@matthijsotterloo commented on GitHub (Mar 30, 2015):

Still

<!-- gh-comment-id:87715986 --> @matthijsotterloo commented on GitHub (Mar 30, 2015): Still
Author
Owner

@jwilsson commented on GitHub (Mar 30, 2015):

I'm running out of ideas here. Have you tried creating a brand new app and using those credentials instead? (which you should anyway since your secret is now out in the open)

@hvt any ideas?

<!-- gh-comment-id:87763663 --> @jwilsson commented on GitHub (Mar 30, 2015): I'm running out of ideas here. Have you tried creating a brand new app and using those credentials instead? (which you should anyway since your secret is now out in the open) @hvt any ideas?
Author
Owner

@hvt commented on GitHub (Apr 1, 2015):

Try: https://gist.github.com/hvt/b1ec30ece40e2fdd872c (of course with the appropriate client ID and client secret)

I really bet it's the trailing slash in your 'redirect URI' (i.e. the Session constructor's third argument). And like @jwilsson says, be sure this trailing slash is both in the code, and at the Spotify application page.

<!-- gh-comment-id:88448627 --> @hvt commented on GitHub (Apr 1, 2015): Try: https://gist.github.com/hvt/b1ec30ece40e2fdd872c (of course with the appropriate client ID and client secret) I really bet it's the trailing slash in your 'redirect URI' (i.e. the `Session` constructor's third argument). And like @jwilsson says, be sure this trailing slash is both in the code, and at the Spotify application page.
Author
Owner

@matthijsotterloo commented on GitHub (Apr 1, 2015):

@hvt Still the same problem....

<!-- gh-comment-id:88449511 --> @matthijsotterloo commented on GitHub (Apr 1, 2015): @hvt Still the same problem....
Author
Owner

@hvt commented on GitHub (Apr 1, 2015):

What about this?

https://gist.github.com/hvt/b1ec30ece40e2fdd872c

<!-- gh-comment-id:88451853 --> @hvt commented on GitHub (Apr 1, 2015): What about this? https://gist.github.com/hvt/b1ec30ece40e2fdd872c
Author
Owner

@matthijsotterloo commented on GitHub (Apr 1, 2015):

@hvt Unable to load the requested file: helpers/input_helper.php

<!-- gh-comment-id:88452640 --> @matthijsotterloo commented on GitHub (Apr 1, 2015): @hvt Unable to load the requested file: helpers/input_helper.php
Author
Owner

@hvt commented on GitHub (Apr 1, 2015):

Now I'm out of options too...

I guess there's truly something wrong with your client ID, client secret, and redirect URL + what information you supplied to Spotify.

<!-- gh-comment-id:88456099 --> @hvt commented on GitHub (Apr 1, 2015): Now I'm out of options too... I guess there's truly something wrong with your client ID, client secret, and redirect URL + what information you supplied to Spotify.
Author
Owner

@jwilsson commented on GitHub (Apr 1, 2015):

Seems like the input helper doesn't exist. I removed it, try this: https://gist.github.com/jwilsson/305ce1bfc740e1f50687

<!-- gh-comment-id:88466154 --> @jwilsson commented on GitHub (Apr 1, 2015): Seems like the `input` helper doesn't exist. I removed it, try this: https://gist.github.com/jwilsson/305ce1bfc740e1f50687
Author
Owner

@matthijsotterloo commented on GitHub (Apr 1, 2015):

@jwilsson Yes and now?

<!-- gh-comment-id:88488812 --> @matthijsotterloo commented on GitHub (Apr 1, 2015): @jwilsson Yes and now?
Author
Owner

@jwilsson commented on GitHub (Apr 1, 2015):

What's your error now?
We're gonna need you to work a bit with us here :)

Have your tried creating a new app at https://developer.spotify.com/my-applications/ and setting your redirect URI to https://beta.cloudmoosic.com/music/spotify/login/ (with a trailing slash) and updating your code with the new credentials?

<!-- gh-comment-id:88495937 --> @jwilsson commented on GitHub (Apr 1, 2015): What's your error now? We're gonna need you to work a bit with us here :) Have your tried creating a new app at https://developer.spotify.com/my-applications/ and setting your redirect URI to https://beta.cloudmoosic.com/music/spotify/login/ (with a trailing slash) and updating your code with the new credentials?
Author
Owner

@matthijsotterloo commented on GitHub (Apr 1, 2015):

@jwilsson Lol maybe :p
Error is still the same! Yes I already did, this is the error again:

Fatal error: Uncaught exception 'SpotifyWebAPI\SpotifyWebAPIException' with message 'Invalid authorization code' in /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php:139 Stack trace: #0 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php(26): SpotifyWebAPI\Request->send('POST', 'https://account...', Array, Array) #1 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Session.php(229): SpotifyWebAPI\Request->account('POST', '/api/token', Array) #2 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/spotify.php(40): SpotifyWebAPI\Session->requestAccessToken('AQDhUZNV5IMYHWF...') #3 [internal function]: Spotify->login() #4 /home/cloudmoosic/www/beta.cloudmoosic.com/system/core/CodeIgniter.php(360): call_user_func_array(Array, Array) #5 /home/cloudmoosic/www/beta.cloudmoosic.com/index.php(232): require_once('/home/cloudmoos...') #6 {main} thrown in /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php on line 139

<!-- gh-comment-id:88499679 --> @matthijsotterloo commented on GitHub (Apr 1, 2015): @jwilsson Lol maybe :p Error is still the same! Yes I already did, this is the error again: Fatal error: Uncaught exception 'SpotifyWebAPI\SpotifyWebAPIException' with message 'Invalid authorization code' in /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php:139 Stack trace: #0 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php(26): SpotifyWebAPI\Request->send('POST', 'https://account...', Array, Array) #1 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Session.php(229): SpotifyWebAPI\Request->account('POST', '/api/token', Array) #2 /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/spotify.php(40): SpotifyWebAPI\Session->requestAccessToken('AQDhUZNV5IMYHWF...') #3 [internal function]: Spotify->login() #4 /home/cloudmoosic/www/beta.cloudmoosic.com/system/core/CodeIgniter.php(360): call_user_func_array(Array, Array) #5 /home/cloudmoosic/www/beta.cloudmoosic.com/index.php(232): require_once('/home/cloudmoos...') #6 {main} thrown in /home/cloudmoosic/www/beta.cloudmoosic.com/application/modules/music/controllers/vendor/Request.php on line 139
Author
Owner

@jwilsson commented on GitHub (Apr 1, 2015):

The second argument to $this->input->get('code') have gone missing, I've added it back here:
https://gist.github.com/jwilsson/305ce1bfc740e1f50687

<!-- gh-comment-id:88505049 --> @jwilsson commented on GitHub (Apr 1, 2015): The second argument to `$this->input->get('code')` have gone missing, I've added it back here: https://gist.github.com/jwilsson/305ce1bfc740e1f50687
Author
Owner

@matthijsotterloo commented on GitHub (Apr 1, 2015):

@jwilsson That's not fixing the error

<!-- gh-comment-id:88506403 --> @matthijsotterloo commented on GitHub (Apr 1, 2015): @jwilsson That's not fixing the error
Author
Owner

@jwilsson commented on GitHub (Apr 3, 2015):

I really don't know where the error is coming from. It seems like CodeIgniter is doing something funky which is out of our control.

I'm closing this for now since it isn't a bug in the library.

<!-- gh-comment-id:89421990 --> @jwilsson commented on GitHub (Apr 3, 2015): I really don't know where the error is coming from. It seems like CodeIgniter is doing something funky which is out of our control. I'm closing this for now since it isn't a bug in the library.
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#13
No description provided.