[GH-ISSUE #88] Issue with basic example and displaying user details #44

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

Originally created by @Pau1fitz on GitHub (Jun 10, 2017).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/88

I am working on the basic examples in the docs which is strangely not showing the user's profile on the front end.

I have taken a couple of approaches but cannot get it working.

The code is the basic example and looks like this:

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    '****',
    '****',
    'test.html'
);

$api = new SpotifyWebAPI\SpotifyWebAPI();

if (isset($_GET['code'])) {
    $session->requestAccessToken($_GET['code']);
    $api->setAccessToken($session->getAccessToken());

    print_r($api->me());
} else {
    $options = [
        'scope' => [
            'user-read-email',
        ],
    ];

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

The url looks like http://url/index.html?code=AQCH5_yl4bOPEvRaMfwQjpdm6esTfkUVwmZ5NEFlD0L9sfmst5-8FmwK0puU_9kI8yOK5i0uLUSYH00dW3kvl_gPCG0ARpgNaAdDoVTF33oOVY-qkh-EG3TRvmYj2u1c9qVAM8ea_g4JL7LOUPyQTdEPSFFwykpU_YCGzOo2grd0jR_Cu6L9DflC-xlappmP_yvFNLiXLzmS-qJIyla1g_NKfYZQJykGqKjozoBnJrBLDcllwNOwQ1pRRo2m

I would have thought the php code would handle exchanging the code for a token but this doesn't appear to be happening.

Originally created by @Pau1fitz on GitHub (Jun 10, 2017). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/88 I am working on the [basic examples in the docs which is strangely not showing the user's profile](https://github.com/jwilsson/spotify-web-api-php/blob/master/docs/examples/access-token-with-authorization-code-flow.md) on the front end. I have taken a couple of approaches but cannot get it working. The code is the basic example and looks like this: ``` require 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session( '****', '****', 'test.html' ); $api = new SpotifyWebAPI\SpotifyWebAPI(); if (isset($_GET['code'])) { $session->requestAccessToken($_GET['code']); $api->setAccessToken($session->getAccessToken()); print_r($api->me()); } else { $options = [ 'scope' => [ 'user-read-email', ], ]; header('Location: ' . $session->getAuthorizeUrl($options)); die(); } ``` The url looks like `http://url/index.html?code=AQCH5_yl4bOPEvRaMfwQjpdm6esTfkUVwmZ5NEFlD0L9sfmst5-8FmwK0puU_9kI8yOK5i0uLUSYH00dW3kvl_gPCG0ARpgNaAdDoVTF33oOVY-qkh-EG3TRvmYj2u1c9qVAM8ea_g4JL7LOUPyQTdEPSFFwykpU_YCGzOo2grd0jR_Cu6L9DflC-xlappmP_yvFNLiXLzmS-qJIyla1g_NKfYZQJykGqKjozoBnJrBLDcllwNOwQ1pRRo2m` I would have thought the php code would handle exchanging the code for a token but this doesn't appear to be happening.
kerem 2026-02-27 19:25:45 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Jun 10, 2017):

Hi!
What's happening when you run the code? Do you get any error messages? Is the code interpreted as PHP (I'm asking since you're using .html-files)?

<!-- gh-comment-id:307572480 --> @jwilsson commented on GitHub (Jun 10, 2017): Hi! What's happening when you run the code? Do you get any error messages? Is the code interpreted as PHP (I'm asking since you're using `.html`-files)?
Author
Owner

@Pau1fitz commented on GitHub (Jun 10, 2017):

I have not got any error messages. I have also tried by using two php files. One of the files step1 was used to to request access to the user's account and nd redirecting them to your app's authorize URL (step2.php)

When I made this attempt I had a step1.php that looked like this:

<?php

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    'CLIENT_ID',
    'CLIENT_SECRET',
    'REDIRECT_URI'
);

$options = [
    'scope' => [
        'playlist-read-private',
        'user-read-private',
    ],
];

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

?>

then my step2.php file looked like this:

<?php

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session('CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URI');
$api = new SpotifyWebAPI\SpotifyWebAPI();

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

// Set the access token on the API wrapper
$api->setAccessToken($accessToken);

print_r($api->me());

?>
<!-- gh-comment-id:307592541 --> @Pau1fitz commented on GitHub (Jun 10, 2017): I have not got any error messages. I have also tried by using two `php` files. One of the files `step1` was used to to request access to the user's account and nd redirecting them to your app's authorize URL (`step2.php`) When I made this attempt I had a `step1.php` that looked like this: ``` <?php require 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session( 'CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URI' ); $options = [ 'scope' => [ 'playlist-read-private', 'user-read-private', ], ]; header('Location: ' . $session->getAuthorizeUrl($options)); die(); ?> ``` then my `step2.php` file looked like this: ``` <?php require 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session('CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URI'); $api = new SpotifyWebAPI\SpotifyWebAPI(); // Request a access token using the code from Spotify $session->requestAccessToken($_GET['code']); $accessToken = $session->getAccessToken(); // Set the access token on the API wrapper $api->setAccessToken($accessToken); print_r($api->me()); ?> ```
Author
Owner

@jwilsson commented on GitHub (Jun 12, 2017):

Hmm, are you using an absolute URL as the redirect uri? For example http://localhost/test.php in both the application you created at Spotify's developer site and in your code? Make sure they're an exact match, trailing slashes and everything. If they differ, that might be the issue.

Otherwise, I don't know what the problem might be. I can't see anything wrong with your code. It should work.

<!-- gh-comment-id:307695451 --> @jwilsson commented on GitHub (Jun 12, 2017): Hmm, are you using an absolute URL as the redirect uri? For example `http://localhost/test.php` in both the application you created at Spotify's developer site and in your code? Make sure they're an exact match, trailing slashes and everything. If they differ, that might be the issue. Otherwise, I don't know what the problem might be. I can't see anything wrong with your code. It should work.
Author
Owner

@Pau1fitz commented on GitHub (Jun 12, 2017):

it worked! typical! don't actually think there was any issue. just wasn't working for some reason :-) thanks for your help!

<!-- gh-comment-id:307716942 --> @Pau1fitz commented on GitHub (Jun 12, 2017): it worked! typical! don't actually think there was any issue. just wasn't working for some reason :-) thanks for your help!
Author
Owner

@jwilsson commented on GitHub (Jun 12, 2017):

Great! Glad that we got it working!

<!-- gh-comment-id:307730192 --> @jwilsson commented on GitHub (Jun 12, 2017): Great! Glad that we got it working!
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#44
No description provided.