Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get an access token Spotify API?

I've been looking at the Spotify api for a few days now and example source code and I still can't figure out how to get an access token to access a user's playlist data. I've gotten to the point where I pull up the login window, the user logs in, and then I receive an authorization code. At this point, I tried doing things like:

window.open("https://accounts.spotify.com/api/token?
grant_type=authorization_code&code="+code+"&redirect_uri=myurl&client_id=3137b15
2f1424defa2c6020ae5c6d444&client_secret=mysecret");

and

$.ajax(
    {
      url: "https://accounts.spotify.com/api/token?grant_type=authorization_code&code="+code+"&redirect_uri=myurl&client_secret=mysecret&client_id=myid", 
      success: function(result){
        alert("foo");
      }
    }
);

But either way I get a result like:

{"error":"server_error","error_description":"Unexpected status: 405"}

instead of a token. I'm sure this is simple but I'm terrible at JS. Please help! Thank you!

(edit) I forgot to mention:

Link to api authentication guide: https://developer.spotify.com/web-api/authorization-guide/

I'm stuck on step 4. I see that there is an alternative method on sending a "header parameter" or cURL request which might work. But seing as how I have no idea how to do these things I've stuck with sending the client_id and client_secret as body request parameters like I did before for the user login/code.

PS: I'm only using this application I'm writing for myself. Is there a way I could hardcode a token in without going through this process instead?

like image 366
Ashwin Gupta Avatar asked Oct 06 '16 04:10

Ashwin Gupta


People also ask

What is a Spotify access token?

The access token allows you to make requests to the Spotify Web API. To do so, you need to include the following header in your API calls: HEADER PARAMETER. VALUE. Authorization.

Why does Spotify say no token provided?

It looks like you are missing your API key or not passing it properly. Save this answer. Show activity on this post. Go to https://developer.spotify.com/console/get-search-item/ to generate a OAuth Token and use the same token in your API call as Authorization Bearer token.

How long does Spotify API token last?

Access tokens issued from the Spotify account service has a lifetime of one hour. If a longer session is desired Spotify account service supports the OAuth Code grant flow.

Does Spotify have a free API?

You can make similar calls through the Web API to retrieve information from the Spotify catalog about artists, tracks and playlists. There is a huge amount of data available, and the best part is that it's free to access.


1 Answers

When the authorization code has been received, you will need to exchange it with an access token by making a POST request to the Spotify Accounts service, this time to its /api/token endpoint:

So you need to make a POST request to the Spotify API, with the parameters in the request body:

$.ajax(
  {
    method: "POST",
    url: "https://accounts.spotify.com/api/token",
    data: {
      "grant_type":    "authorization_code",
      "code":          code,
      "redirect_uri":  myurl,
      "client_secret": mysecret,
      "client_id":     myid,
    },
    success: function(result) {
      // handle result...
    },
  }
);

(As a sidenote, "Unexpected status: 405" refers to the HTTP status code 405 Method Not Allowed, which indicates that the request method you tried—a GET request—is not allowed on that URL.)

like image 178
Frxstrem Avatar answered Oct 06 '22 16:10

Frxstrem