簡體   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