gpt4 book ai didi

api - 415 从请求 token Spotify API 回来

转载 作者:行者123 更新时间:2023-12-05 08:54:39 25 4
gpt4 key购买 nike

我正在尝试从 Spotify api 接收 token 。不幸的是,我一直收到 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
};

最佳答案

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

关于api - 415 从请求 token Spotify API 回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48947586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com