简体   繁体   English

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

[英]Spotify Web API - Client Credentials - Accessing a users playlists

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获取播放列表文档: https : //developer.spotify.com/web-api/get-playlists-tracks/

Spotify getting client credentials documentation: https://developer.spotify.com/web-api/authorization-guide/ Spotify获取客户端凭证文档: 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: 其次,我尝试使用https://github.com/jwilsson/spotify-web-api-php客户端凭据流(文档: http : //jwilsson.github.io/spotify-web-api-php/authorization。 html ),实际上是复制该页面底部的代码:

    <?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 这给了我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(); 我也尝试使用$api = new SpotifyWebAPI\\SpotifyWebAPI();创建API变量$api = new SpotifyWebAPI\\SpotifyWebAPI(); but this says I need user information/ tokens. 但这说我需要用户信息/令牌。

Thanks. 谢谢。

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(); 我也尝试使用$ api = new SpotifyWebAPI \\ SpotifyWebAPI();创建API变量。 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 查看代码( Session类SpotifyWebapi类 ),看起来确实应该通过执行以下操作进行设置

$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. 设置完成后,最好像在示例代码中一样使用getUserPlaylists方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM