Struggling with Spotify API Authentication: 400 Error for Bearer Token

I’m having trouble with the Spotify API while trying to fetch my top artists and tracks. I’ve set up the authorization flow using the official examples but I’m hitting a roadblock.

After successfully logging in and accessing my basic info, I’m attempting to retrieve my top artists. However, I’m getting a 400 error saying “Only valid bearer authentication supported”.

Here’s a simplified version of my code:

app.get('/fetch_favorite_artists', (req, res) => {
  const encodedAuth = Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
  const requestOptions = {
    url: 'https://api.spotify.com/v1/me/top/artists',
    headers: {
      'Authorization': `Basic ${encodedAuth}`
    }
  };

  request.post(requestOptions, (error, response, body) => {
    if (!error && response.statusCode === 200) {
      const favoriteArtists = body.favorite_artists;
      res.json({ favoriteArtists });
    }
  });
});

I’ve double-checked my client ID and secret, but I’m still stumped. Any ideas on what I’m doing wrong with the authentication?

ur using basic auth instead of bearer token. for /me/top/artists endpoint, u need to use the access token u got from initial oauth flow. change ur header to:

‘Authorization’: Bearer ${accessToken}

also, use GET not POST for that endpoint. that should fix it!

I’ve worked extensively with the Spotify API, and I can confirm that the issue lies in your authentication method. The /me/top/artists endpoint requires OAuth authentication, not Basic Auth.

Here’s what you need to do:

  1. Obtain an access token through the OAuth flow.
  2. Use that token in your API requests.

Modify your code like this:

app.get('/fetch_favorite_artists', (req, res) => {
  const accessToken = // Your OAuth access token
  const requestOptions = {
    url: 'https://api.spotify.com/v1/me/top/artists',
    headers: {
      'Authorization': `Bearer ${accessToken}`
    },
    json: true
  };

  request.get(requestOptions, (error, response, body) => {
    if (!error && response.statusCode === 200) {
      res.json({ favoriteArtists: body.items });
    } else {
      res.status(response.statusCode).json({ error: body.error });
    }
  });
});

Remember to handle token expiration and implement a refresh mechanism. This should resolve your 400 error.

I ran into a similar issue when I first started working with the Spotify API. The key here is understanding the difference between authentication methods. You’re using Basic Authentication, which is meant for obtaining the access token, not for making API requests.

Here’s what you need to do:

  1. Use your client ID and secret to get an access token first.
  2. Then use that access token as a Bearer token for your API requests.

Your code should look more like this:

app.get('/fetch_favorite_artists', (req, res) => {
  const accessToken = // Get this from your OAuth flow
  const requestOptions = {
    url: 'https://api.spotify.com/v1/me/top/artists',
    headers: {
      'Authorization': `Bearer ${accessToken}`
    },
    json: true
  };

  request.get(requestOptions, (error, response, body) => {
    if (!error && response.statusCode === 200) {
      const favoriteArtists = body.items;
      res.json({ favoriteArtists });
    }
  });
});

Also, don’t forget to handle token expiration and refresh your access token when needed. Good luck with your project!