415 请求令牌 Spotify API 返回

Jak*_*zak 2 api spotify axios

我正在尝试从 Spotify api 接收令牌。不幸的是我一直收到 415。你能帮助我并让我知道我做错了什么吗?

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
};
Run Code Online (Sandbox Code Playgroud)

Łuk*_*zak 5

415错误代码与错误的内容类型或内容编码问题相关,( https://httpstatuses.com/415 )

我不知道axios,但请看一下 Spotify github 上的示例https://github.com/spotify/web-api-auth-examples/blob/master/authorization_code/app.js#L74

根据github上的这个问题(https://github.com/spotify/web-api/issues/321),尝试使用content-type'Content-Type': 'application/x-www-form-urlencoded'

有 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) {
});
Run Code Online (Sandbox Code Playgroud)