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

JavaScript

Thom Benjamin
Thom Benjamin
10,494 Points

How to ask authorization when using an API with JavaScript's fetch()?

Hi everyone!

I finished the Front End course and wanted more practice with fetching data from API's, so I decided to use the one Spotify has.

I kept getting error 401, which means I didn't have access. Now I made an account on their developer website, and I have a Client ID and a secret key, but the next step is asking for an authorization code using these things in combination with POST. It also says I have to do this on the server side, does this mean you can't use API's with JavaScript unless you're using a server side language like PHP or Node.js?

We never learned this in the course, and upon searching for Authorisation in the library, there's nothing. Can someone please explain how I can access API's that need authorization, like most of them do? I tried Googling and looking for tutorials, but I haven't found a good one yet.

Thank you so much in advance.

2 Answers

Did you go through Spotify's tutorial?

Thom Benjamin
Thom Benjamin
10,494 Points

Thanks for your reply. No, because this was using Node and I haven't worked with that yet. I thought it could be done with only plain JavaScript. Is Node.js required? Cause in that case I'll take the Node.js course first!