jwilsson/spotify-web-api-php

How to set scopes when using Client Credentials method

Closed this issue · 2 comments

Hi @jwilsson,

I am trying to create a simple script to return only $api->getMyCurrentTrack(); by using client credentials but without scopes it returns always HTTP ERROR 500 I checked the docs but i was unable to find a way to make this work?

Thanks,
B

I am trying to do everything on the same file and the same request, is this okay!

require 'vendor/autoload.php';

use SpotifyWebAPI\Session;
use SpotifyWebAPI\SpotifyWebAPI;

// Function to request a new access token and fetch track data
function fetchTrackData() {
    $session = new Session(
    	'', // CLIENT_ID
    	'', // CLIENT_SECRET
    );
    $session->requestCredentialsToken();
    $accessToken = $session->getAccessToken();

	$options = [
		'scope' => [
			'user-read-email',
			'user-read-currently-playing',
			'user-read-playback-state',
		],
	];

    // Create SpotifyWebAPI instance and set access token
    $api = new SpotifyWebAPI();
    $api->setAccessToken($accessToken);

    return $api->me();
}

// Fetch track data
echo '<pre>';
print_r(fetchTrackData());
echo '</pre>';

Hey!
If you want to use scopes and access user information you need to use the Authorization Code or Proof Key for Code Exchange (PKCE) (PKCE is the recommended one for new applications) flows. The Client Credentials flow can only be used to fetch information about songs, artists, and so on.

Hope this clears things up!