Announcements

Help Wizard

Step 1

NEXT STEP

Unauthorized Error on Get Current User Data

Solved!

Unauthorized Error on Get Current User Data

My auth code to get auth token:

 

const requestAuthToken = async () => {

await fetch("https://accounts.spotify.com/api/token",

{ body: `grant_type=client_credentials&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}`,

headers: { "Content-Type": "application/x-www-form-urlencoded", },

method: "POST", })

.then((response) => response.json())

.then((data) => { setAuthToken(data.access_token);

navigate("/home");

}) };

 

This is code that fetchs data from /me api:

 

const getUserData = async (token: string) => {

await fetch("https://api.spotify.com/v1/me",

{ headers: { Authorization: "Bearer " + token, } })

.then((response) => response.json())

.then((data) => {

console.log(data);

setUser(User.fromJSON(data)); })

.catch((error) => { console.log(error); }); }

 

The auth function is working so I can get the auth token and save it. But when I try to get users data it is returning 401 Unauthorized.

 

Why?

Reply

Accepted Solutions
Marked as solution

Since the Client Credentials flow does not include authorization, only endpoints that do not access user information can be accessed. Therefore, in this case, you should use a different Authorization method that lets you log in.

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

View solution in original post

Marked as solution

2 Replies
Marked as solution

Since the Client Credentials flow does not include authorization, only endpoints that do not access user information can be accessed. Therefore, in this case, you should use a different Authorization method that lets you log in.

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.
Marked as solution

Problem is solved. Here is the Stack Overflow post:

https://stackoverflow.com/questions/77598436/spotify-web-api-401-unauthorized

Suggested posts

Let's introduce ourselves!

Hey there you,   Yeah, you! 😁   Welcome - we're glad you joined the Spotify Community!   While you here, let's have a fun game and get…

ModeratorStaff / Moderator/ 4 years ago  in Social & Random