[GH-ISSUE #232] "code" index not defined in callback.php? #164

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

Originally created by @amanuelanteneh on GitHub (Jun 6, 2021).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/232

I'm trying to follow the authorization code flow example from here

My auth.php is basically the same as the docs:

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    'clientid',
    'secret',
    'http://localhost/callback.php/'
);

$state = $session->generateState();
$options = [
    'scope' => [
        'playlist-read-private',
        'user-read-private',
    ],
    'state' => $state,
];

header('Location: ' . $session->getAuthorizeUrl($options));
die();

And my callback.php is:

require 'vendor/autoload.php';


$session = new SpotifyWebAPI\Session(
    'clientid',
    'secret',
    'http://localhost/callback.php/'
);

// Request a access token using the code from Spotify
$session->requestAccessToken($_GET['code']);

$accessToken = $session->getAccessToken();
$refreshToken = $session->getRefreshToken();

// Send the user along and fetch some data!
header('Location: index.php');
die();

But I keep getting this error: Undefined index: code in {projectPath}\callback.php on line 12.
I'm not sure why the code index seems to be null. I tried changing $_GET to $_REQUEST like another issue suggested but that yielded the same error.

Originally created by @amanuelanteneh on GitHub (Jun 6, 2021). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/232 I'm trying to follow the authorization code flow example from [here](https://github.com/jwilsson/spotify-web-api-php/blob/9063d2f18934319b71393fb1780de32d45897df9/docs/examples/access-token-with-authorization-code-flow.md) My auth.php is basically the same as the docs: ``` require 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session( 'clientid', 'secret', 'http://localhost/callback.php/' ); $state = $session->generateState(); $options = [ 'scope' => [ 'playlist-read-private', 'user-read-private', ], 'state' => $state, ]; header('Location: ' . $session->getAuthorizeUrl($options)); die(); ``` And my callback.php is: ``` require 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session( 'clientid', 'secret', 'http://localhost/callback.php/' ); // Request a access token using the code from Spotify $session->requestAccessToken($_GET['code']); $accessToken = $session->getAccessToken(); $refreshToken = $session->getRefreshToken(); // Send the user along and fetch some data! header('Location: index.php'); die(); ``` But I keep getting this error: `Undefined index: code in {projectPath}\callback.php on line 12.` I'm not sure why the code index seems to be null. I tried changing `$_GET` to `$_REQUEST` like another issue suggested but that yielded the same error.
kerem 2026-02-27 19:26:23 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@amanuelanteneh commented on GitHub (Jun 6, 2021):

If anyone else is having this issue, for some reason when I include the extra "/" at the end of my redirect URI this error occurs. However when I omit the "/" and instead use: http://localhost/callback.php as the redirect URI the issue resolves itself. Also make sure to omit the "/" when you whitelist the URI on your apps dashboard as well!

<!-- gh-comment-id:855325297 --> @amanuelanteneh commented on GitHub (Jun 6, 2021): If anyone else is having this issue, for some reason when I include the extra "/" at the end of my redirect URI this error occurs. However when I omit the "/" and instead use: `http://localhost/callback.php` as the redirect URI the issue resolves itself. Also make sure to omit the "/" when you whitelist the URI on your apps dashboard as well!
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#164
No description provided.