Search code examples
javascriptangularspotifyionic6

Error 'invalid_client' when using JavaScript and Spotify API with Ionic 6 and Angular


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!


Solution

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