Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get Spotify Access Token (client credentials) with PHP and cURL
<?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;
?>
@ahallora

This comment has been minimized.

Copy link
Owner Author

@ahallora ahallora commented Jul 9, 2014

I know it's basic, but I struggled to figure out how to retrieve the access token from the Spotify web service as the only example provided by Spotify was written in node.js. Hopefully this will save you the time and hassle I went through to get this simple script to work. :)

@thiagoguarnieri

This comment has been minimized.

Copy link

@thiagoguarnieri thiagoguarnieri commented Mar 21, 2016

Excellent

@Bugs1012

This comment has been minimized.

Copy link

@Bugs1012 Bugs1012 commented Jun 26, 2016

thanks. i was struggling too.

@tmdevde

This comment has been minimized.

Copy link

@tmdevde tmdevde commented Jun 9, 2017

thank you!

@ludasolo

This comment has been minimized.

Copy link

@ludasolo ludasolo commented Jun 21, 2017

thank you!

@angeloliva

This comment has been minimized.

Copy link

@angeloliva angeloliva commented Sep 29, 2017

I was very stressful owing to Spotify Oauth.
Many Thanks.
🥇

@alxbarron

This comment has been minimized.

Copy link

@alxbarron alxbarron commented Feb 8, 2018

Thanks, still working. You made my day easier, I was going to use a library for cURL then I found this.

@Mcgurk125

This comment has been minimized.

Copy link

@Mcgurk125 Mcgurk125 commented Jun 13, 2018

Does this still work? - It does not seem to work for me! Can somebody else try?

@MarcoCadei

This comment has been minimized.

Copy link

@MarcoCadei MarcoCadei commented Jun 22, 2018

Thank you a lot! It worked perfectly for me!

@gustavo9601

This comment has been minimized.

Copy link

@gustavo9601 gustavo9601 commented Aug 13, 2018

Thank you so much !!!!

@Lucasmg37

This comment has been minimized.

Copy link

@Lucasmg37 Lucasmg37 commented Jul 2, 2019

Nice!

@pablocoronel

This comment has been minimized.

Copy link

@pablocoronel pablocoronel commented Jan 23, 2020

Thank you!

@Wondarar

This comment has been minimized.

Copy link

@Wondarar Wondarar commented Feb 13, 2020

Thanks much!

@alamaxjashek

This comment has been minimized.

Copy link

@alamaxjashek alamaxjashek commented Sep 6, 2020

I have an error:

HTTP/1.1 400 Bad Request
www-authenticate: Bearer realm="spotify", error="invalid_request", error_description="Only valid bearer authentication supported"
access-control-allow-origin: *
access-control-allow-headers: Accept, App-Platform, Authorization, Content-Type, Origin, Retry-After, Spotify-App-Version, X-Cloud-Trace-Context
access-control-allow-methods: GET, POST, OPTIONS, PUT, DELETE, PATCH
access-control-allow-credentials: true
access-control-max-age: 604800
content-type: application/json
Content-Length: 99
strict-transport-security: max-age=31536000
x-content-type-options: nosniff
date: Sun, 06 Sep 2020 01:49:21 GMT
server: envoy
Via: HTTP/2 edgeproxy, 1.1 google
Alt-Svc: clear


{
  "error": {
    "status": 400,
    "message": "Only valid bearer authentication supported"
  }
}

Who can help me?

@Wondarar

This comment has been minimized.

Copy link

@Wondarar Wondarar commented Sep 7, 2020

@0Blanck0

This comment has been minimized.

Copy link

@0Blanck0 0Blanck0 commented Sep 10, 2020

How do I add the scope?

$scopes = array( 'user-read-playback-state', 'user-read-playback-position' );

@owalid

This comment has been minimized.

Copy link

@owalid owalid commented Jan 28, 2021

Hi @ahallora your gist helped me a lot, thank you very much!

But I spent some time to translate it into nodejs, I made a gist like you: https://gist.github.com/owalid/cf7a2425d9733b6aee80832f6fbc660c

Do not hesitate to look at
Hoping that this will help
Peace ;)

@joehari9

This comment has been minimized.

Copy link

@joehari9 joehari9 commented Feb 24, 2021

Thank you brother

@Mikzael

This comment has been minimized.

Copy link

@Mikzael Mikzael commented Mar 5, 2021

You sir are so kind to share this knowledge, I was having trouble with this, i was passing to CURLOPT_POSTFIELDS an array but I didnt realized it required a string... thank you again sir.

@f13dev

This comment has been minimized.

Copy link

@f13dev f13dev commented Apr 11, 2021

Still working nicely in 2021 - One minor recommendation, add 'curl_close($ch);' before the final echo for best practices.

@juliebugmtl

This comment has been minimized.

Copy link

@juliebugmtl juliebugmtl commented Jul 25, 2021

Superb, this helped me with another API service too. Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment