[GH-ISSUE #222] Issues with client credentials authorization flow #154

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

Originally created by @ericlove02 on GitHub (Jan 28, 2021).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/222

I am trying to run a simple set up to return a track, but am getting this error everytime:

Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: Unauthorized.

The code I have is in auth.php:

<?php 

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    'my client id',
    'my secret'
);

$session->requestCredentialsToken();
$accessToken = $session->getAccessToken();

// Store the access token somewhere. In a database for example.
session_start();
$_SESSION["token"] = $accessToken;

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

and app.php:

<?php
require 'vendor/autoload.php';

$api = new SpotifyWebAPI\SpotifyWebAPI();

if(!(isset($_SESSION))){
	session_start();
}
// Fetch the saved access token from somewhere. A session for example.
$api->setAccessToken($_SESSION["token"]);

// It's now possible to request data about the currently authenticated user
print_r(
    $api->me()
);

// Getting Spotify catalog data is of course also possible
print_r(
    $api->getTrack('7EjyzZcbLxW7PaaLua9Ksb')
);

All of this is copied from an example from this github, except the $SESSION storing.

Originally created by @ericlove02 on GitHub (Jan 28, 2021). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/222 I am trying to run a simple set up to return a track, but am getting this error everytime: `Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: Unauthorized.` The code I have is in auth.php: ``` <?php require 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session( 'my client id', 'my secret' ); $session->requestCredentialsToken(); $accessToken = $session->getAccessToken(); // Store the access token somewhere. In a database for example. session_start(); $_SESSION["token"] = $accessToken; // Send the user along and fetch some data! header('Location: app.php'); die(); ``` and app.php: ``` <?php require 'vendor/autoload.php'; $api = new SpotifyWebAPI\SpotifyWebAPI(); if(!(isset($_SESSION))){ session_start(); } // Fetch the saved access token from somewhere. A session for example. $api->setAccessToken($_SESSION["token"]); // It's now possible to request data about the currently authenticated user print_r( $api->me() ); // Getting Spotify catalog data is of course also possible print_r( $api->getTrack('7EjyzZcbLxW7PaaLua9Ksb') ); ``` All of this is copied from an example from this github, except the $SESSION storing.
kerem 2026-02-27 19:26:21 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Jan 28, 2021):

Hi!
I think the issue here is the call to $api->me() which requires an access token tied to a user which the client credentials flow won't grant. I'd try and remove that line and see if it works better.

<!-- gh-comment-id:769356363 --> @jwilsson commented on GitHub (Jan 28, 2021): Hi! I think the issue here is the call to `$api->me()` which requires an access token tied to a user which the client credentials flow won't grant. I'd try and remove that line and see if it works better.
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#154
No description provided.