The code below uses the request library authentication to connect to the Spotify API.
import requests client_id = # Enter your client id here client_secret = # Enter your client secret here grant_type = 'client_credentials' #Request based on Client Credentials Flow from https: //developer.spotify.com/web-api/authorization-guide/ #Request body parameter: grant_type Value: Required.Set it to client_credentials body_params = { 'grant_type': grant_type } url = 'https://accounts.spotify.com/api/token' response = requests.post(url, data = body_params, auth = (client_id, client_secret)) print response
Last modified: May 13, 2022, by MDN contributors
415 Unsupported Media Type
Well, seems I found the answer based on this question : ,Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.,Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.,Here is my script for an auth request to Spotify but it returns an error. I tried changing the Content-Type, but that doesn't seem to cut it. Here is my code:
Here is my script for an auth request to Spotify but it returns an error. I tried changing the Content-Type, but that doesn't seem to cut it. Here is my code:
$spot_api_client = 'client';
$spot_api_secret = 'secret';
$spot_api_redirect = 'myurl';
if (isset($_GET['state']) && isset($_COOKIE['stateKey']) && $_COOKIE['stateKey'] == $_GET['state']) {
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://accounts.spotify.com/api/token",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'redirect_uri' => urlencode($spot_api_redirect),
),
CURLOPT_HTTPHEADER => array(
'Accept' => '*/*',
'User-Agent' => 'runscope/0.1',
'Authorization' => 'Basic '.base64_encode($spot_api_client.
':'.$spot_api_secret),
'Content-Type' => 'application/json'
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
}
Copy code
Well, seems I found the answer based on this question :
$url = 'https://accounts.spotify.com/api/token';
$method = 'POST';
$spot_api_redirect = 'myurl';
$credentials = "client:secret";
$headers = array(
"Accept: */*",
"Content-Type: application/x-www-form-urlencoded",
"User-Agent: runscope/0.1",
"Authorization: Basic ".base64_encode($credentials));
$data = 'grant_type=authorization_code&code='.$_GET['code'].
'&redirect_uri='.urlencode($spot_api_redirect);
if (isset($_GET['state']) && isset($_COOKIE['stateKey']) && $_COOKIE['stateKey'] == $_GET['state']) {
unset($_COOKIE['stateKey']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
print_r($response);
}
Copy code
Just in case of node.js / request use, point out "WTF" tag :)
request({
method: "POST",
url: "https://accounts.spotify.com/api/token",
json: true,
headers: {
"Content-Type": "application/x-www-form-urlencoded", // WTF!
"Authorization": "Basic " + new Buffer(client_id + ":" + client_secret).toString('base64')
},
body: "grant_type=client_credentials"
},
function(err, response, body) {
if (err) {
cb(err);
} else {
if (body.hasOwnProperty("access_token")) {
access_token = body.access_token;
cb(null);
} else {
cb(body);
}
}
}
);
Copy code