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

Receiving 400 error with authorizationCodeGrant #116

Open mdoxey06 opened 7 years ago

mdoxey06 commented 7 years ago

Hi,

I'm trying to create a playlist (after the user logs in), but after trying to generate a new access_token using authorizationCodeGrant(), I get "{ [WebapiError: Bad Request] name: 'WebapiError', message: 'Bad Request', statusCode: 400 }". I am using the same code that is generated after the callback on user login.

spotifyApi.authorizationCodeGrant(code)
           .then(function(data) {
                      spotifyApi.setAccessToken(data.body['access_token']);
                      spotifyApi.setRefreshToken(data.body['refresh_token']);
                      spotifyApi.createPlaylist(userObj['id'], playlistName, { public : false })
                          .then(function(data) {
                              sendTextMessage(sender, "success! created playlist: " + JSON.stringify(data.body))
                          }, function(err) {
                                     console.log('Something went wrong createPlaylist!', err);
                         });
           }, function(err) {
                           console.log('Something went wrong authorizationCodeGrant!', err);
});

Any help is appreciated, thank you!

orchestor commented 7 years ago

I have same problem too.

sowiecki commented 7 years ago

I was getting a similar error until I added redirectUri to my SpotifyWebApi instance. Like so:

const spotifyApi = new SpotifyWebApi({
  clientId: '123',
  clientSecret: 'abc',
  redirectUri: 'https://example.com/callback'
});

spotifyApi.authorizationCodeGrant(code).then(doStuffWithData) // no more 400
JMPerez commented 7 years ago

@mdoxey06 did you manage to solve it?

mdoxey06 commented 7 years ago

I have found that when I put the spotifyApi.createPlaylist call after the spotify.Api.authorizationCodeGrant call (instead of nested inside the .then()), it works for me.

JMPerez commented 7 years ago

@mdoxey06 but then createPlaylist would run before setting the access token, wouldn't it?

mdoxey06 commented 7 years ago

The odd thing I'm finding when testing it now is that I receive a 400 on the first try, and then it tries again and successfully creates the playlist (after only running once). Would there be any reason for this?

orchestor commented 7 years ago

Me too. Today works here.

JMPerez commented 7 years ago

@mdoxey06 AFAIK there is no code in the library to retry this. Do you mind sharing a code snippet?

mdoxey06 commented 7 years ago

@JMPerez Here is a code snippet from my project that is currently working:

// code stored from previous user login
spotifyApi.authorizationCodeGrant(code)
  .then(function(data) {
    spotifyApi.setAccessToken(data.body['access_token']);
    spotifyApi.setRefreshToken(data.body['refresh_token']);
  }, function(err) {
    console.log('Something went wrong authorizationCodeGrant!', err);
});

spotifyApi.createPlaylist(userObj['id'], playlistName, { public : false })
  .then(function(data) {
    playlistId = data.body.id;
    console.log(sender, "success! created playlist: " + JSON.stringify(data.body.id));
  }, function(err) {
    console.log('Something went wrong createPlaylist!', err);
  });
jmfortunatojr commented 7 years ago

I'd like to bump this as well - I am randomly getting failures here on a production app. Seems like this just fails randomly with a 400 bad request error.

cbaudouinjr commented 5 years ago

Would also like to bump, still having issues with this.

Nfinley commented 5 years ago

Same here running into similar issues

brunano21 commented 4 years ago

do I have to generate an authorizationCcode every time I run my script?