415 从请求令牌 Spotify API 回来 [英] 415 coming back from requesting a token Spotify API

查看:48
本文介绍了415 从请求令牌 Spotify API 回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Spotify api 接收令牌.不幸的是,我一直收到 415.你能帮我看看我做错了什么吗?

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
};

推荐答案

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

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

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

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

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

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'

有使用 axios 的例子

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) {
});

这篇关于415 从请求令牌 Spotify API 回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆