router.get('/spotifyLogin', (req,res) => {
const state = generateRandomString(16);
const scope = 'user-read-recently-played';
const queryParams = querystring.stringify({
response_type: 'code',
client_id: client_id,
scope: scope,
redirect_uri: redirect_uri,
state: state
});
res.redirect(`https://accounts.spotify.com/authorize?${queryParams}`);
});
router.get('/callback', (req,res) => {
const authorizationCode = req.query.code || null;
const state = req.query.state || null;
axios({
method: 'post',
url: 'https://accounts.spotify.com/api/token',
data: querystring.stringify({
grant_type: 'authorization_code',
code: authorizationCode,
redirect_uri: redirect_uri,
}),
headers: {
'content-type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${new Buffer.from(`${client_id}:${client_secret}`).toString('base64')}`,
},
})
.then(response => {
if (response.status === 200) {
res.send(response.data)
} else {
res.send(response);
}
})
.catch(error => {
res.send(error);
});
});
我正在定制express api服务器https://developer.spotify.com/documentation/general/guides/authorization/code-flow/上执行spotify授权代码流。
当我向令牌url发出post请求时,我应该在www.example.com中得到一个json对象response.data,但是我得到了奇怪的字符Screenshot of response.data
3条答案
按热度按时间h5qlskok1#
这是一个与axios版本1.2.0相关的错误。您可以通过将axios降级到1.1.3来修复此错误。有关此错误的更多信息:https://github.com/axios/axios/issues/5298
eni9jsuy2#
是的,这是
"axios": "1.2.0"
中的错误。请检查this issue。将
'accept-encoding': '*'
添加到标头中修复了它。请参阅此documentation,以了解
accept-encoding
。alen0pnh3#
对于axios请求,将
accept-encoding
头设置为*
解决了这个问题。在您的情况下,应该如下所示: