Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

415 coming back from requesting a token Spotify API

Tags:

axios

spotify

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
};
like image 593
Jakub Luczak Avatar asked Dec 05 '25 04:12

Jakub Luczak


1 Answers

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) {
});
like image 66
Łukasz Szewczak Avatar answered Dec 10 '25 13:12

Łukasz Szewczak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!