Last active
March 17, 2023 09:44
-
-
Save ahallora/4aac6d048742d5de0e65 to your computer and use it in GitHub Desktop.
Get Spotify Access Token (client credentials) with PHP and cURL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$client_id = '<insert your spotify app client id>'; | |
$client_secret = '<insert your spotify app client secret>'; | |
$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, 'grant_type=client_credentials' ); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.base64_encode($client_id.':'.$client_secret))); | |
$result=curl_exec($ch); | |
echo $result; | |
?> |
Superb, this helped me with another API service too. Thanks so much!
Thanks a lot!
How do I add the scope?
$scopes = array( 'user-read-playback-state', 'user-read-playback-position' );
where to add
hi! This has worked for me several years, but now it suddenly gives the following error: Error: Forbidden
Your client does not have permission to get URL /api/token from this server. Anyone else facing the same issue? Any ideas what has been changed and how to fix this?
Thanks!
Still worked for me!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still working nicely in 2021 - One minor recommendation, add 'curl_close($ch);' before the final echo for best practices.