繁体   English   中英

Curl GET 请求到 Spotify 授权 API

[英]Curl GET Request to the spotify Authorization API

我需要一些关于我对 Spotify API 的 Curl GET 请求的帮助。 API 具有三种不同的方式/端点来获得授权。

我阅读了一些文章,以找到发送请求的正确语法。 但我总是得到一个错误。 如果我将 url 发布到我的 brwoser 中,它可以完美运行,也可以使用重定向 uri。 但它不适用于 Curl GET 请求。

这听起来很愚蠢,但我在过去的三天里都在解决这个问题。

我的代码:

<?php
$client_id = 'myClientID';
$redirect_url = 'http://mywebsite/first/page.php';
$scope = 'user-read-private%20user-read-email';

$data = array(
    'client_id' => $client_id,
    'response_type' => 'code',
    'redirect_uri' => $redirect_url,
    'state' => stateHash(), // Create a random hash
    'scope' => $scope,
    'show_dialog' => 'true'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.spotify.com/authorize' . http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
$result=curl_exec($ch);
echo $result;

来自 API 的错误向我展示了这一点: 在此处输入图片说明

或者我得到一个“1”作为回应。

我希望我能得到一些不错的提示:)

有一个 Spotify web API 包尝试使用它

composer require jwilsson/spotify-web-api-php

在使用 Spotify Web API 之前,您需要在Spotify 的开发者站点上创建一个应用程序

显示用户个人资料的简单示例:

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    'CLIENT_ID',
    'CLIENT_SECRET',
    'REDIRECT_URI'
);

$api = new SpotifyWebAPI\SpotifyWebAPI();

if (isset($_GET['code'])) {
    $session->requestAccessToken($_GET['code']);
    $api->setAccessToken($session->getAccessToken());

    print_r($api->me());
} else {
    $options = [
        'scope' => [
            'user-read-email',
        ],
    ];

    header('Location: ' . $session->getAuthorizeUrl($options));
    die();
}

有关更多说明和示例,请查看文档

暂无
暂无

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

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