Jay*_* Yu 2 node.js express spotify-app axios
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);
});
});
Run Code Online (Sandbox Code Playgroud)
我正在自定义 Express api 服务器上执行 Spotify 授权代码流程 https://developer.spotify.com/documentation/general/guides/authorization/code-flow/
当我向 token url 发出 post 请求时,我应该在我的response.data中得到一个json对象,但是我得到了奇怪的字符 response.data的屏幕截图
小智 6
将axios 请求的accept-encoding标头设置为解决了我的问题。*
在你的情况下,应该看起来像这样:
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')}`,
'accept-encoding': '*'
},
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1553 次 |
| 最近记录: |