Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"415 Error" when querying Spotify for tokens

I've been trying to recreate the spotify oauth connection in MeteorJS. I've gotten as far as requesting the access and refresh tokens, but I keep getting a 415 error now. Here is the relevant code:

 var results = HTTP.post(
                'https://accounts.spotify.com/api/token',
                {
                    data: {
                        code: code,
                        redirect_uri: redirectURI,
                        grant_type: 'authorization_code',
                        client_id: clientID,
                        client_secret: clientSecret
                    },
                    headers: {
                        'Content-Type':'application/json'
                    }
                }
            );

I can't seem to find any other good documentation of the problem and the code in this demo:

https://github.com/spotify/web-api-auth-examples/tree/master/authorization_code

works perfectly.

like image 222
Pete.Mertz Avatar asked Aug 21 '14 17:08

Pete.Mertz


3 Answers

I had a similar problem (but in Java). The analogous solution was

                    headers: {
                        'Content-Type':'application/x-www-form-urlencoded'
                    }
like image 166
Kirby Avatar answered Nov 20 '22 18:11

Kirby


You need to use params instead of data when sending the JSON object. Related question: Unsupported grant type error when requesting access_token on Spotify API with Meteor HTTP

like image 2
José M. Pérez Avatar answered Nov 20 '22 17:11

José M. Pérez


I have successfully tried getting the access token from Spotify, using the below function. As you can see, you don't need to specify Content-Type, but just need to use params instead of data (as far as axios is concerned). Also make sure that you first combine the client id and the client secret key with a ":" in between them and then convert the combined string into base 64.

let getAccessToken = () => {
  let options = {
    url: 'https://accounts.spotify.com/api/token',
    method: 'POST',
    headers: {
      // 'Content-Type':'application/x-www-form-urlencoded',
      'Authorization': `Basic <base64 encoded client_id:client_secret>`
    },
    params: {
      grant_type: 'client_credentials'
    }
  }
  axios(options)
  .then((resp) => {
    console.log('resp', resp.data)
  })
  .catch((err) => {
    console.log('ERR GETTING SPOTIFY ACCESS TOKEN', err);
  })
}
like image 2
Farhan Ali Avatar answered Nov 20 '22 16:11

Farhan Ali