[英]Spotify oauth2 with PHP curl. How to get authorization code?
我曾嘗試遵循 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 塊,嵌入到我的頁面中,並帶有以下文本:“錯誤糟糕,出了點問題。請重試或查看我們的幫助區域。”
然而有趣的是,如果我將完整的查詢放入我的搜索欄中,它會完全按照我的預期工作,將我帶到授權頁面,並在接受后重定向回我的回調 url,我的訪問代碼作為參數在 url 中。
為什么在我的 PHP function 中無法獲得想要的效果?
任何提示將不勝感激。 我知道我可以使用 GitHub 上的 php spotify api 包裝器,但我想自己做這個作為學習項目。
您必須遵循 3 步過程。
通過簡單地從上面的數據構建查詢,並將其附加到授權 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 獲取授權碼,然后使用 ZF6E57C9DE709E45FEB0D955358F5354 繼續授權流程。
<?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>';
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.