【问题标题】:Spotify oauth2 with PHP curl. How to get authorization code?带有 PHP curl 的 Spotify oauth2。如何获取授权码?
【发布时间】:2021-01-24 02:31:12
【问题描述】:

我已尝试遵循 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 函数不能得到想要的效果?

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

spotify error response

【问题讨论】:

  • 您已经在 spotify 上创建了一个应用程序?我在您的代码中没有看到任何 CLIENT SECRET 请参阅:github.com/jwilsson/spotify-web-api-php/blob/master/docs/…
  • 问题解决了吗?我也有同样的情况。从导航栏访问,Spotify 重定向到我的页面,我可以处理代码,但只能这样 /:
  • $redirect_uri 是否与您在应用中为重定向 URI 设置的 URI 匹配?
  • 这里也一样......有人解决了这个问题吗??

标签: php oauth-2.0 spotify


【解决方案1】:

您必须遵循 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 中获取授权码,并使用 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>';
    }
}

【讨论】:

    猜你喜欢
    • 2013-09-30
    • 2015-08-08
    • 2019-05-11
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 2018-05-21
    • 2016-11-08
    • 2021-04-24
    相关资源
    最近更新 更多