spotify / web-api

This issue tracker is no longer used. Join us in the Spotify for Developers forum for support with the Spotify Web API ➡️ https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer
983 stars 79 forks source link

Getting "Invalid Redirect URI" when trying to get an access token from Authorization Code Flow? #1106

Open ranveerkunal opened 5 years ago

ranveerkunal commented 5 years ago

I have my Redirect URI correctly set up in the console.

Issue found on December 11th 2018.

Endpoint(s):

Scope(s):

Steps to reproduce:

let authOptions = { url: 'https://accounts.spotify.com/api/token', form: { 'grant_type': 'authorization_code', 'code': code, 'redirect_uri': redirectUri }, headers: { 'Authorization': 'Basic ' + (new Buffer(clientId + ':' + clientSecret).toString('base64')) }, json: true };

console.log(authOptions);
request.post(authOptions, function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
});

Expected behaviour:

return token

Actual behaviour:

400 Server Error (consistently)

kapekost commented 5 years ago

I managed to make a similar scenario work do you see any description at the error?

I think you're missing the content type in your header: ('Content-Type', 'application/x-www-form-urlencoded')

  1. Have your application request refresh and access tokens; Spotify returns access and refresh tokens When the authorization code has been received, you will need to exchange it with an access token by making a POST request to the Spotify Accounts service, this time to its /api/token endpoint: POST https://accounts.spotify.com/api/token The body of this POST request must contain the following parameters encoded in ´application/x-www-form-urlencoded as defined in the OAuth 2.0 specification:

relevant documentation: https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow

bih commented 5 years ago

Typically one of the most common bugs we see is the Redirect URIs have a trailing slash in one place, and don't in others. They must be the exact same Redirect URL including the trailing slash everywhere, and most times that seems to resolve the issue.

Can you let me know if this has resolved your issue @ranveerkunal?