question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi there,

I’m trying to make a request, but I really don’t get what I’m doing wrong, error messages like shown below keep showing up:

Something went wrong on auth! { [WebapiError: Bad Request] name: 'WebapiError', message: 'Bad Request', statusCode: 400 }

and

Something went wrong on playlists! { [WebapiError: Unauthorized] name: 'WebapiError', message: 'Unauthorized', statusCode: 401 }

This afternoon I got it working once, but now the access token is expired or something I guess? But when I’ll run the refresh function you posted in the authorization section I’ll get:

Could not refresh access token { [WebapiError: Bad Request] name: 'WebapiError', message: 'Bad Request', statusCode: 400 }

How can I debug this? What can I do? If it helps, here is my SpotifyClient class:

import SpotifyWebApi from 'spotify-web-api-node';

class SpotifyClient {
  constructor(username) {
    this.username = username;

    this.spotifyApi = new SpotifyWebApi({
      clientId : 'CLIENT_ID',
      clientSecret : 'CLIENT_SECRET',
      redirectUri : 'http://localhost:6001/cv',
      accessToken : 'HERE_WAS_THE_CODE'
    });

    // this.setAuthorization();
    this.refreshAuthorization();
  }

  setAuthorization() {
    const scopes = ['user-read-private', 'user-read-email'];
    const state = 't3st';

    const authorizeURL = this.spotifyApi.createAuthorizeURL(scopes, state);
    console.log(authorizeURL);

    const code = 'HERE_WAS_THE_CODE_FROM_AUTH_URL';

    this.spotifyApi.authorizationCodeGrant(code)
      .then(function(data) {
        console.log('The token expires in ' + data.body['expires_in']);
        console.log('The access token is ' + data.body['access_token']);
        console.log('The refresh token is ' + data.body['refresh_token']);

        // Set the access token on the API object to use it in later calls
        this.spotifyApi.setAccessToken(data.body['access_token']);
        this.spotifyApi.setRefreshToken(data.body['refresh_token']);
      }, function(err) {
        console.log('Something went wrong on auth!', err);
      });
  }

  refreshAuthorization() {
    this.spotifyApi.refreshAccessToken()
      .then(function(data) {
        console.log('The access token has been refreshed!');

        // Save the access token so that it's used in future calls
        this.spotifyApi.setAccessToken(data.body['access_token']);
      }, function(err) {
        console.log('Could not refresh access token', err);
      });
  }

  getPlaylistById(callback) {
    console.info('spotify client get');

    this.spotifyApi.getPlaylist(this.username, '2tj5TA5I1QySyHI3YdpXYj')
      .then(function(data) {
        // console.log('Some information about this playlist', data.body);
        console.info('spotify client did return');
        // return data.body;

        callback(null, data.body);
      }, function(err) {
        console.log('Something went wrong on playlists!', err);
      });
  }
}

export default SpotifyClient;

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:7
  • Comments:13

github_iconTop GitHub Comments

6reactions
JMPerezcommented, Mar 28, 2017

@tetreault No problem. We’ve all been there… several times a week. Computer is a weird science. Sometimes I would like to go and grow potatoes instead.

2reactions
develmusacommented, Nov 28, 2016

I had the same problem.

I couldn’t solve it within this wrapper. Now i use https://github.com/jaredhanson/passport-github to authenticate. If you want some sample code feel free to contact me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

400 Bad Request - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request ...
Read more >
How to Fix a 400 Bad Request Error (Causes and Fixes) - Kinsta
The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived...
Read more >
How to Fix a 400 Bad Request Error: 8 Easy Methods
The 400 bad request error is an HTTP status code that describes an error caused by an invalid request. Thus, the server can't...
Read more >
400 Bad Request Error: What It Is and How to Fix It
The 400 Bad Request Error is an HTTP response status code indicating that the server was unable to process the request sent by...
Read more >
HTTP Error 400 Bad Request: What It Is and How to Fix It
“400 Bad Request” is a general error that indicates that your browser sends a request to the website's server and the server can't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found