dongzha0813 2015-06-12 19:11
浏览 68
已采纳

Spotify Web API - 客户端凭据 - 访问用户播放列表

I'm trying to access users playlist tracks by using the client credentials flow.

Spotify getting playlist documentation: https://developer.spotify.com/web-api/get-playlists-tracks/

Spotify getting client credentials documentation: https://developer.spotify.com/web-api/authorization-guide/

First question, is it possible to get a users playlist tracks using client credentials flow? I'm using this flow since I'm unable to pop up a login box for the user to login.

Secondly, I've tried using https://github.com/jwilsson/spotify-web-api-php client credentials flow (Docs: http://jwilsson.github.io/spotify-web-api-php/authorization.html) by practically copying the code at the bottom of that page:

    <?php

include('vendor/autoload.php');

$session = new SpotifyWebAPI\Session('Tobys client id', 'Tobys secret', 'http://localhost/callback');
// Request a access token with optional scopes
$scopes = array(
    'playlist-read-private',
    'user-read-private'
);

$session->requestCredentialsToken($scopes);
$accessToken = $session->getAccessToken(); // We're good to go!

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

$playlists = $api->getUserPlaylists('USER_ID', array(
    'limit' => 5
));

foreach ($playlists->items as $playlist) {
    echo '<a href="' . $playlist->external_urls->spotify . '">' . $playlist->name . '</a> <br>';
}

This gives me Notice: Undefined variable: api in /var/www/html/dev/testing.php on line 16

I've also tried creating the API variable using $api = new SpotifyWebAPI\SpotifyWebAPI(); but this says I need user information/ tokens.

Thanks.

  • 写回答

1条回答 默认 最新

  • du5114 2015-06-12 20:35
    关注

    First question, is it possible to get a users playlist tracks using client credentials flow?

    Yes, retrieving tracks for a playlist doesn't require user authentication as part of the access token.

    I've also tried creating the API variable using $api = new SpotifyWebAPI\SpotifyWebAPI(); but this says I need user information/ tokens.

    Looking at the code (Session class, SpotifyWebapi class), it does look like you should set this up by doing

    $api = new SpotifyWebAPI\SpotifyWebAPI();
    $session = new SpotifyWebAPI\Session($clientId, $clientSecret, $redirectUri);
    $api->setAccessToken($session->getAccessToken());
    

    When that's set up you should be good to use the getUserPlaylists method like you're doing in your example code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测