Jakub Luczak
Jakub Luczak

Reputation: 41

415 coming back from requesting a token Spotify API

I'm trying to receive a token from Spotify api. Unfortunately I keep on receiving 415. Could you help me and let me know what am I doing wrong?

const axios = require('axios');

const getToken = (code) => {
    return axios({
        method: 'post',
        url:'https://accounts.spotify.com/api/token',
        form: {
            code,
            grant_type :'authorization_code',
            redirect_uri: process.env.SPOTIFY_REDIRECT
        },
        headers: {
            'Authorization': 'Basic ' + (new Buffer(process.env.SPOTIFY_ID + ':' + process.env.SPOTIFY_SECRET).toString('base64')),
            'Content-Type': 'application/json'
        }
    }).then(token => {
        return token;
    }).catch(e=> {
        console.log(e);
        return e.response;
    });
};

module.exports = {
    getToken
};

Upvotes: 2

Views: 3134

Answers (4)

Biciklius
Biciklius

Reputation: 1

If you making API call from client side (browser), try this solution:

getTokken() {
const urlSpotify = "https://accounts.spotify.com/api/token";
axios({
  method: "post",
  url: urlSpotify,
  data: "grant_type=client_credentials",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/x-www-form-urlencoded",
  },
  auth: {
    username: process.env.REACT_APP_SPTID_KEY, // User ID
    password: process.env.REACT_APP_SPCS_KEY,  // User Secret
  },
})
  .then((response) => {
    console.log(response);
  })
  .catch((err) => console.log(err));
}

Upvotes: 0

Gerardo Gonzalez
Gerardo Gonzalez

Reputation: 111

After spending one hour trying to figure out how to get the token, I came up with this answer! :)

const axios = require('axios');
const express = require('express');
const app = express();

const client_id= 'YOURCLIENTID';
const client_secret = 'YOURCLIENTSECRET';

app.get('/api/token', (req, res) => {
  axios({
    method: 'post',
    url: 'https://accounts.spotify.com/api/token',
    headers: {
    'Authorization': 'Basic ' + (new Buffer.from(client_id + ':' + client_secret).toString('base64')),        
    'Content-Type': 'application/x-www-form-urlencoded'
    },
    params: {
      grant_type: 'client_credentials'
    },
    json: true,
  })
    .then(body => {
      res.send(body.data.access_token);
    })
    .catch(e => {
      console.log(e.response.data);
    });
});

app.listen(3000, () => {
  console.log('Server Listening on port 3000');
});

Upvotes: 2

Łukasz Szewczak
Łukasz Szewczak

Reputation: 1881

415 error code is related to problem with wrong content type or content encoding, (https://httpstatuses.com/415)

I do not know axios but please take a look on the example on spotify github https://github.com/spotify/web-api-auth-examples/blob/master/authorization_code/app.js#L74

According to this issue on github (https://github.com/spotify/web-api/issues/321), try to use content-type 'Content-Type': 'application/x-www-form-urlencoded'

There is example withs axios

axios({
    url: "https://accounts.spotify.com/api/token",
    method: "post",
    params: {
        grant_type: "client_credentials"
    },
    headers: {
        "Accept": "application/json",
        "Content-Type": "application/x-www-form-urlencoded"
    },
    auth: {
        username: "YOUR-CLIENT-ID",
        password: "YOUR-CLIENT-SECRET"
    }
}).then(function (response) {
    console.log(response);
}).catch(function (error) {
});

Upvotes: 5

Jakub Luczak
Jakub Luczak

Reputation: 41

It works!!! What I've done was: - change Content-Type for 'application/x-www-form-urlencoded' - client_id and client_secret were taken from header and posted before grant_type in body - changed 'data' to 'params'

const axios = require('axios');

const getToken = (code) => {
    return axios({
        method: 'post',
        url:'https://accounts.spotify.com/api/token',
        params: {
            client_id: process.env.SPOTIFY_ID,
            client_secret: process.env.SPOTIFY_SECRET,
            code,
            grant_type :'authorization_code',
            redirect_uri: process.env.SPOTIFY_REDIRECT
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    }).then(token => {
        return token;
    }).catch(e=> {
        return e.response.data;
    });
};

And it resulted with a beautiful looking token \m/

Upvotes: 2

Related Questions