Spotify oauth2与PHP curl.如何获得授权码?



我尝试按照spotify的授权代码流程操作,但是在获得用户授权的第一关就失败了。

我已经尝试过使用PHP curl。

如果你看一下我的代码,我做错了什么?

$url           = 'https://accounts.spotify.com/authorize';
$redirect_uri  = urlencode( site_url() );
$curl = curl_init();

curl_setopt( $curl, CURLOPT_URL, $url);
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_HTTPGET, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, 'state=' );
curl_setopt( $curl, CURLOPT_POSTFIELDS, 'response_type=code' );
curl_setopt( $curl, CURLOPT_POSTFIELDS, 'scope=playlist-read-private%20playlist-modify-private' );
curl_setopt( $curl, CURLOPT_POSTFIELDS, 'client_id=' . $this->client_id );
curl_setopt( $curl, CURLOPT_POSTFIELDS, 'redirect_uri=' . $redirect_uri );

curl_exec( $curl );
curl_close( $curl );

$fullquery     = 'https://accounts.spotify.com/authorize?response_type=code&state=&client_id=CLIENT_ID&scope=playlist-read-private%20playlist-modify-private&redirect_uri=http%3A%2F%2Flocalhost%3A8888%2Fspotifyapi';

运行此命令将返回一个来自spotify的html块,嵌入到我的页面中,并带有文本:"Error Oops!出错了,请再试一次或查看我们的帮助区域。">

有趣的是,如果我把完整的查询放到我的搜索栏中,它就像我期望的那样工作,带我到一个授权页面,接受后,重定向到我的回调url,我的访问码作为url中的参数。

为什么我不能在我的PHP函数中得到想要的效果?

任何提示将非常感激。我知道GitHub上有一个php spotify api包装器,我可以使用,但我想自己做一个学习项目。

spotify错误响应

你必须遵循三个步骤。

我通过简单地从上面的数据构建查询,并将其附加到授权url,并在页面上添加一个链接,成功地完成了上面概述的第一步。这样的:

$data = array(
'client_id'     => $client_id,
'redirect_uri'  => $redirect_url,
'scope'         => $scope,
'response_type' => $response_type,
);
$oauth_url = 'https://accounts.spotify.com/authorize?' . http_build_query( $data );
?>
<a href="<?php echo $oauth_url; ?>">Login</a>

单击链接后,您将返回到重定向url,在那里您可以从url获得授权代码,并使用curl继续进行授权流程。

<?php if ( isset( $_GET['code'] ) && ! isset( $_SESSION['spotify_access'] ) ) {
$data = array(
'redirect_uri' => $redirect_url,
'grant_type'   => 'authorization_code',
'code'         => $_GET['code'],
);
$ch            = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://accounts.spotify.com/api/token' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $data ) );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: Basic ' . base64_encode( $client_id . ':' . $client_secret ) ) );
$result = json_decode( curl_exec( $ch ) );

curl_close( $ch );
if ( isset( $result->access_token ) ) {
header( 'Location: http://localhost:8888/spotify/getprofile.php?access=' . $result->access_token );
}
}

最后,您可以获取用户数据:

<?php if ( isset( $_GET['access'] ) ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://api.spotify.com/v1/me' );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type:application/json', 'Authorization: Bearer ' . $_GET['access'] ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$userprofile = json_decode( curl_exec( $ch ) );
curl_close( $ch );
echo '<pre>';
var_dump( $userprofile );
echo '</pre>';
if ( $userprofile->id ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://api.spotify.com/v1/users/' . $userprofile->id . '/playlists' );
curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $_GET['access'] ) );
$playlists = json_decode( curl_exec( $ch ) );
curl_close( $ch );
echo '<pre>';
var_dump( $playlists );
echo '</pre>';
}
}

最新更新