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.11k stars 499 forks source link

authorizationCodeGrant returns bad request - Is my token invalid ? #131

Closed RHeijnen closed 7 years ago

RHeijnen commented 7 years ago

Sorry if this is a duplicate issue,

Working from the examples and tutorials, I used the Authorization code flow pattern like so;

var authorizationCode = '<insert authorization code>';

var scopes = ['playlist-modify-private', 'playlist-modify-public'],
    redirectUri = 'http://localhost:8888/callback',
    clientId = 'xxx',
    state = 'some-state-of-my-choice';

var spotifyApi = new SpotifyWebApi({
  redirectUri : redirectUri,
  clientId : clientId,
});

// Create the authorization URL
var authorizeURL = spotifyApi.createAuthorizeURL(scopes, state);
console.log(authorizeURL);

Ive been useing express to catch my authentication code when a 'user' clicks on the authentication URL like so:

app.get('/callback', function (req, res) {
  console.log(req.query.code);
  authorizationCode = req.query.code;
  initSpotify(authorizationCode)
});

that trows the authentication code into..

initSpotify= function(authenticationCode){
  spotifyApi.authorizationCodeGrant(authenticationCode).then(function(data) {
    spotifyApi.setAccessToken(data.body['access_token']);
    spotifyApi.setRefreshToken(data.body['refresh_token']);
   }, function(err) {
     console.log('Something went wrong authorizationCodeGrant!', err);
  });
}`

Console output: Something went wrong authorizationCodeGrant! { [WebapiError: Bad Request] name: 'WebapiError', message: 'Bad Request', statusCode: 400 }

An example of the URI token/authentication code (is there a difference?) that is returned looks like this

==Token:== AQCpp5p03ypSieV38cEGewsvWuMhC50J09oCAlMQ-ucqWGSNhygRs0mIbdMlCHK4_kkv7m2OUJBxJ791aW9kby8NprKaSv3HjB9QMTa7gMHHFx1PsTbEIfjrhO35ONf2UiGWQYB2oEBc_mORSKyvnJ3qyHaViQ-Lyk-7ieIt0ri1HuzDBODB8LuuBVmpbbD9ZnlO7NYJfYrV5ieK6oY 0BNYuCimxTl3XjrsLM99ZFa5p4-0OyUV8tf7bWpucC3VFpE2zQpE

This seems a lot larger than others that I've been seeing snooping around, so I suppose this is where my problem lies?

Any ideas would be greatly appreciated, sincerly.

RHeijnen commented 7 years ago

problem:

var spotifyApi = new SpotifyWebApi({
  clientId : 'x',  
  clientSecret : 'x', <---Pretty sure this is missing in the tutorial
  redirectUri : 'http://localhost:8888/callback'
});

oh well, I can sleep happy.