[GH-ISSUE #96] Example on getMyCurrentTrack() #53

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

Originally created by @template01 on GitHub (Aug 30, 2017).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/96

Hello,
Was wondering if you could supply an example on returning the current track.

Here's what i'm currently trying out:

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

$session = new SpotifyWebAPI\Session(
    'xxxxxxxxBLURRED_OUT_HERExxxxxxxxxxxxxx',
    'xxxxxxxxBLURRED_OUT_HERExxxxxxxxxxxxxx',
    'xxxxxxxxBLURRED_OUT_HERExxxxxxxxxxxxxx'
);

$api = new SpotifyWebAPI\SpotifyWebAPI();

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

    $getMyCurrentTrack = $api->getMyCurrentTrack();

} else {
    $options = [
        'scope' => [
            'user-read-email',
        ],
    ];

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

This sadly does not work and i get this error returned:

Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: Permissions missing in /home/lasbian/tools/spotify/vendor/jwilsson/spotify-web-api-php/src/Request.php:38 Stack trace: #0 /home/lasbian/tools/spotify/vendor/jwilsson/spotify-web-api-php/src/Request.php(222): SpotifyWebAPI\Request->parseBody(Object(stdClass), 401) #1 /home/lasbian/tools/spotify/vendor/jwilsson/spotify-web-api-php/src/Request.php(107): SpotifyWebAPI\Request->send('GET', 'https://api.spo...', '', Array) #2 /home/lasbian/tools/spotify/vendor/jwilsson/spotify-web-api-php/src/SpotifyWebAPI.php(794): SpotifyWebAPI\Request->api('GET', '/v1/me/player/c...', Array, Array) #3 /home/lasbian/tools/spotify/index.php(17): SpotifyWebAPI\SpotifyWebAPI->getMyCurrentTrack() #4 {main} thrown in /home/lasbian/tools/spotify/vendor/jwilsson/spotify-web-api-php/src/Request.php on line 38

All the best,

Lasse

Originally created by @template01 on GitHub (Aug 30, 2017). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/96 Hello, Was wondering if you could supply an example on returning the current track. Here's what i'm currently trying out: ``` <?php require 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session( 'xxxxxxxxBLURRED_OUT_HERExxxxxxxxxxxxxx', 'xxxxxxxxBLURRED_OUT_HERExxxxxxxxxxxxxx', 'xxxxxxxxBLURRED_OUT_HERExxxxxxxxxxxxxx' ); $api = new SpotifyWebAPI\SpotifyWebAPI(); if (isset($_GET['code'])) { $session->requestAccessToken($_GET['code']); $api->setAccessToken($session->getAccessToken()); $getMyCurrentTrack = $api->getMyCurrentTrack(); } else { $options = [ 'scope' => [ 'user-read-email', ], ]; header('Location: ' . $session->getAuthorizeUrl($options)); die(); } ?> ``` This sadly does not work and i get this error returned: ``` Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: Permissions missing in /home/lasbian/tools/spotify/vendor/jwilsson/spotify-web-api-php/src/Request.php:38 Stack trace: #0 /home/lasbian/tools/spotify/vendor/jwilsson/spotify-web-api-php/src/Request.php(222): SpotifyWebAPI\Request->parseBody(Object(stdClass), 401) #1 /home/lasbian/tools/spotify/vendor/jwilsson/spotify-web-api-php/src/Request.php(107): SpotifyWebAPI\Request->send('GET', 'https://api.spo...', '', Array) #2 /home/lasbian/tools/spotify/vendor/jwilsson/spotify-web-api-php/src/SpotifyWebAPI.php(794): SpotifyWebAPI\Request->api('GET', '/v1/me/player/c...', Array, Array) #3 /home/lasbian/tools/spotify/index.php(17): SpotifyWebAPI\SpotifyWebAPI->getMyCurrentTrack() #4 {main} thrown in /home/lasbian/tools/spotify/vendor/jwilsson/spotify-web-api-php/src/Request.php on line 38 ``` All the best, Lasse
kerem closed this issue 2026-02-27 19:25:48 +03:00
Author
Owner

@jwilsson commented on GitHub (Aug 30, 2017):

Hi!
Sure thing, I'll add an example.

But I think your problem is due to some missing scopes, add these two along withuser-read-email and I think it'll work better:

$options = [
    'scope' => [
        'user-read-email',
        'user-read-currently-playing',
        'user-read-playback-state',
    ],
];
<!-- gh-comment-id:325950002 --> @jwilsson commented on GitHub (Aug 30, 2017): Hi! Sure thing, I'll add an example. But I think your problem is due to some missing scopes, add these two along with`user-read-email` and I think it'll work better: ```php $options = [ 'scope' => [ 'user-read-email', 'user-read-currently-playing', 'user-read-playback-state', ], ];
Author
Owner

@jwilsson commented on GitHub (Aug 30, 2017):

Just for future reference, I've added an example in 4d8e6226eb.

<!-- gh-comment-id:325958665 --> @jwilsson commented on GitHub (Aug 30, 2017): Just for future reference, I've added an example in 4d8e6226eb06644616d9da25ab39898c628ec8ae.
Author
Owner

@template01 commented on GitHub (Aug 30, 2017):

Awesome! Thanks for the help! And thanks for this wrapper 👍

<!-- gh-comment-id:325983348 --> @template01 commented on GitHub (Aug 30, 2017): Awesome! Thanks for the help! And thanks for this wrapper :+1:
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#53
No description provided.