Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Invalid client

Solved!

Invalid client

I started making my own music display app. I chose to use the Spotify API, I don't have a premium subscription (can the problem arise because of this?) . I registered the app, got a secret client and a client ID. Here's my code.
async getData(){
let result = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization': 'Basic' + btoa(this.client_id + ':' + this.client_secret)
},
body: 'grant_type=client_cred

Why do I get an error 400, error: 'invalid_client' response from the server
Reply

Accepted Solutions
Marked as solution

hi

i don't think this is because you don't have premium account in sdk docs its stated on first line that its only for premium users in web api there is no mention of that, anywhere.

the code doesn't look complete so i can't be sure but if i am correct then the function is inside some class? please post the whole code there could be some bug else but most bugs in code are because we forget about the simple things. Are you sure that you have correct client_id and client_secret pasted in? Somebody had similar problem and it was just a typo (https://community.spotify.com/t5/Spotify-for-Developers/INVALID-CLIENT-invalid-client/td-p/5410735).

if you are sure that its correct can you please try this code, i have tested this code and i got the access_token.

async function getToken(){
    const authOptions = {
        url: `https://accounts.spotify.com/api/token`,
        method: 'POST',
        headers: {
            'Authorization': 'Basic ' + btoa(client_id + ':' + client_secret),
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: 'grant_type=client_credentials'
    };

    fetch(authOptions.url, authOptions)
        .then(response => response.json())
        .then(data => {
            console.log(data);
        }).catch(error => {
            console.error(error);
    });
}

getToken();

 if this doesn't work can you run it in browser console and look at the network tab and provide more info. i am sure that it will be useful debug info even for more competent people than i am.

i hope that i have helped at least a little qwq

 

View solution in original post

3 Replies
Marked as solution

hi

i don't think this is because you don't have premium account in sdk docs its stated on first line that its only for premium users in web api there is no mention of that, anywhere.

the code doesn't look complete so i can't be sure but if i am correct then the function is inside some class? please post the whole code there could be some bug else but most bugs in code are because we forget about the simple things. Are you sure that you have correct client_id and client_secret pasted in? Somebody had similar problem and it was just a typo (https://community.spotify.com/t5/Spotify-for-Developers/INVALID-CLIENT-invalid-client/td-p/5410735).

if you are sure that its correct can you please try this code, i have tested this code and i got the access_token.

async function getToken(){
    const authOptions = {
        url: `https://accounts.spotify.com/api/token`,
        method: 'POST',
        headers: {
            'Authorization': 'Basic ' + btoa(client_id + ':' + client_secret),
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: 'grant_type=client_credentials'
    };

    fetch(authOptions.url, authOptions)
        .then(response => response.json())
        .then(data => {
            console.log(data);
        }).catch(error => {
            console.error(error);
    });
}

getToken();

 if this doesn't work can you run it in browser console and look at the network tab and provide more info. i am sure that it will be useful debug info even for more competent people than i am.

i hope that i have helped at least a little qwq

 

Since [Client Credentials] do not include authorization, only endpoints that do not access user information can be accessed.

The page of the Get Currently Playing Track endpoint access user data, and it mentions Authorization scopes.

So implementing the Authorization Code Flow is required to let your app work.

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.

Thank you, you helped a lot in solving my question, now everything works! I am very grateful to you

Suggested posts

Type a product name