0

I'm encountering an issue while using JavaScript and the Spotify API in my Ionic 6 with Angular project. Here's the code snippet I'm using:

async function getToken() {
    const clientid = 'xxxxx';
    const clientsecret = 'xxxxx';
    const buf = new Buffer.from(`${clientid}:${clientsecret}`);

    const result = await fetch('https://accounts.spotify.com/api/token', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': 'Basic' + buf.toString('base64')
        },
        body: 'grant_type=client_credentials'
    });

    const data = await result.json();
    return data;
}

However, I'm getting the following error: "invalid_client". Can anyone provide some guidance on how to resolve this issue?

Thank you in advance for your help!

  • Does this answer your question? [Spotify API {'error': 'invalid\_client'} Authorization Code Flow \[400\]](https://stackoverflow.com/questions/53567858/spotify-api-error-invalid-client-authorization-code-flow-400) – fubar May 06 '23 at 14:21
  • And what should the code variable be? Also, im getting another error: requests is not defined. Sorry in quite new to this stuff. Thanks for the response. – Giannis Petsis May 06 '23 at 14:27

1 Answers1

0

EDIT: The problem was that the 'Basic' was missing a space, so its 'Basic '.