Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

APIs

Justin Sanchez
Justin Sanchez
7,131 Points

How do you use an API token?

Hi guys, so I'm a little stuck on figuring out how to use an API token from Spotify. I'm trying to develop a Spotify playlist generator where you type in the artist's name and it will create a little playlist for you. I'm able to interact with their server but I keep getting the 401 unauthorized code. So I have the API token and now I don't know how to have it authenticate me. Any ideas?

I read you have to do something with the headers but I'm also stumped on that.

app.apiUrl = 'https://api.spotify.com/v1';
var accessToken = '[private_info]';

//Go to spotify and get the artists
app.searchArtist = (artistName) => $.ajax({
    url: `${app.apiUrl}/search`,
    headers: {
        'Authorization':'Bearer ' + accessToken
    },
    method: 'GET',
    dataType: 'json',
    data: {
        q: artistName,
        type: 'artist'
    }
});

3 Answers

Dale Severude
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,349 Points

Hi Justin,

A 401 error means you have invalid credentials. With your accessToken variable, I am assuming you have a valid key somewhere that you are passing into this variable. If not, paste it into the variable, but don't show it here.

It is possible your key doesn't work for a few hours after receiving it. See the Spotify help section for examples of setting up your key correctly.

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

Justin Sanchez
Justin Sanchez
7,131 Points

This was actually case. *Developer tools' Network tab said it had expired which was why I was getting the 401 error. Resolved it by refreshing and obtaining a new key. Thank you!

Steven Parker
Steven Parker
229,921 Points

:mailbox_with_mail: Hi, I got your request.

I haven't set up a Spotify client myself, but looking briefly at the documentation, I noticed that the authorization scheme has several layers. Your user keys are apparently different from the access key used to make queries, but can be used to obtain a temporary access key.

Take a look at this page describing the authorization code flow and see if you possibly skipped a step or two in setting up the authorization for a query.

Justin Sanchez
Justin Sanchez
7,131 Points

Yes the access token doesn't last very long. Sadly only lasts for a short amount of time. Thanks for the flow chart I'll have to look into this more myself.

So far from what I understand is you have to meet the parameters of each request. For example, if you wanted to request and artist's album(https://developer.spotify.com/web-api/get-artists-albums/), you would have to check the documentation and fill out the headers required in order to make the request.