thelinmichael / spotify-web-api-node

A Node.js wrapper for Spotify's Web API.
http://thelinmichael.github.io/spotify-web-api-node/
MIT License
3.1k stars 499 forks source link

Bad request #95

Open rnnyrk opened 7 years ago

rnnyrk commented 7 years ago

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;
develmusa commented 7 years ago

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.

tetreault commented 7 years ago

@thelinmichael can you provide some feedback? I feel like this issue warrants a response.

tetreault commented 7 years ago

@develmusa I would love a gist if you could provide one. I'm banging my head against the wall as well.

The only functionality I need to perform is to fetch and dump public playlists that are under my user account (which is tied to my spotify dev account).

JMPerez commented 7 years ago

@Ronnyrr did you manage to solve your issue? By looking at your code there is one thing that can create some problems, which is hardcoding the authorization code. Even if you generated it properly, you can only use it once to exchange with an access token.

rnnyrk commented 7 years ago

@JMPerez No unfortunately I never was able to fix this issue. Like @develmusa mentioned, I managed to fix it using passport.

tetreault commented 7 years ago

@JMPerez I know i've been all over the place but I finally got the access token being generated properly. If possible - i'd like your feedback on this.

I store my API keys in a .env file. I have a config.js file that pulls in the values from the .env file via process.env.FIELD_NAME. Therein lies the issue. If i hard code my API keys when we instantiate a new SpotifyWebApi it is functional but otherwise all attempts fail.

This is why I suppose it took so many methods till I found this because I've never had any issue reading in values from a .env file before using the dotenv module.

I even scanned the .env file for any invisible characters that somehow got introduced thru VIM that were messing things up but there was nothing there.

Furthermore I have Clarifai API keys stored in the .env file and there was zero issue getting those API keys from that file into the instantiation of a new Clarifai App. The issue here was only present with the spotify keys.

I know the keys are correct too because I copied them from the .env file and pasted them into my server.js file - I didn't re-copy them from the Spotify Dev dashboard.

JMPerez commented 7 years ago

@tetreault Great that you managed to get it working! It still sounds quite strange. There shouldn't be any difference between reading the values from a file or hardcoding them. For the wrapper it's just some values.

tetreault commented 7 years ago

Right! @JMPerez to make matters even more confusing I printed out the keys from .env -> config.js -> server.js.

For the spotify Client ID only there is an extra single quote being added to the end of the spotify Client ID. However, the Client Secret is fine, totally unscathed. I checked all through - no extra single quote anywhere plus again the other API keys in the .env file dont have this issue...

Very curious, I wish I knew why this was happening. But I appreciate you helping me through all my spam about this issue in various threads! haha Thank you for your patience.

JMPerez commented 7 years ago

@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.

tetreault commented 7 years ago

Hah @JMPerez my backup for when I grow too weary of this is to become a welder. Thanks again for your hard work on this node wrapper - hopefully all goes well from here and I dont have to bug you again :)

pannous commented 6 years ago

This error appears when there is a typo in the credentials. Please add "check your credentials" to the error message!

n1ru4l commented 6 years ago

This error popped up in my sentry logs and I do not really know what caused it. A better description would be way better.

patrick-motard commented 4 years ago

Pretty sure I'm running into the same issue and not sure how to move forward.