"415 Error" when querying Spotify for tokens
Asked Answered
R

4

15

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.

Reeves answered 21/8, 2014 at 17:44 Comment(1)
yes, I get the same problem. Hmm.Salazar
S
22

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

                    headers: {
                        'Content-Type':'application/x-www-form-urlencoded'
                    }
Salazar answered 18/12, 2014 at 21:40 Comment(1)
This is the only solution that worked at 2022 (:Sidetrack
R
3

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);
  })
}
Righthander answered 11/5, 2021 at 21:16 Comment(0)
B
2

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

Bipartisan answered 27/1, 2015 at 10:16 Comment(0)
E
-5

If youre doing this clientside its not working because you're not allowed to post to another domain from the client side because of the same origin policy.

If this is server-side I'd recommend using a pre-existing spotify api npm module instead of writing your own requests. There are plenty of spotify api implementations on npmjs.org.

Use arunoda's npm package for integrating npm packages in your meteor application

Enterprising answered 22/8, 2014 at 9:35 Comment(1)
This is a server side request, as client side would both not allow the request, because of the same origin policy as you point out, and because the HTTP library in meteor does not allow synchronous requests on the client. Using a prexisting package is not an option, as you'll see that the repository you pointed to only has three working functions.Reeves

© 2022 - 2024 — McMap. All rights reserved.