简体   繁体   English

Curl GET 请求到 Spotify 授权 API

[英]Curl GET Request to the spotify Authorization API

I need some help with my Curl GET Request to the Spotify API.我需要一些关于我对 Spotify API 的 Curl GET 请求的帮助。 The API has three different ways/endpoints to get an authorization. API 具有三种不同的方式/端点来获得授权。

I read some articles, to find the correct syntax to send the request.我阅读了一些文章,以找到发送请求的正确语法。 But i always get an error.但我总是得到一个错误。 If i post the url into my brwoser it works perfectly, also with the redirect uri.如果我将 url 发布到我的 brwoser 中,它可以完美运行,也可以使用重定向 uri。 But it doesnt work with the Curl GET Request.但它不适用于 Curl GET 请求。

It sounds stupid, but i spend the last three days with this Problem.这听起来很愚蠢,但我在过去的三天里都在解决这个问题。

My code:我的代码:

<?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;

The error from the API Shows me this:来自 API 的错误向我展示了这一点: 在此处输入图片说明

or i got an "1" as response.或者我得到一个“1”作为回应。

I hope that i get some nice tips :)我希望我能得到一些不错的提示:)

There is a package for Spotify web API try using that有一个 Spotify web API 包尝试使用它

composer require jwilsson/spotify-web-api-php

Before using the Spotify Web API, you'll need to create an app at Spotify's developer site.在使用 Spotify Web API 之前,您需要在Spotify 的开发者站点上创建一个应用程序

Simple example displaying a user's profile:显示用户个人资料的简单示例:

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();
}

For more instructions and examples, check out the documentation .有关更多说明和示例,请查看文档

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

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