Spotify API授权代码流返回'unsupported
我正在尝试使用Node.js应用程序访问Spotify Web API。我已经指定grant_type
为authorization_code
但收到unsupported_grant_type
错误描述grant_type must be client_credentials, authorization_code or refresh_token
。
据我所知,我的post
请求格式正确,值都正确。不知道还有什么要检查。
app.post('/auth', (req, res)=>{
const auth = Buffer
.from(`${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`)
.toString('base64');
axios.post(token_uri, {}, {
params: {
'grant_type': 'authorization_code',
'code': req.body.code,
'redirect_uri': redirect_uri,
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET
}, headers: {
'Authorization': `Basic ${auth}`,
'Content-Type':'application/x-www-form-urlencoded'
}
})
.then(res=>{
console.log(res.data)
})
.catch(err=>{
console.log(err)
})
})
回答如下:
您正确设置了内容类型,但是您要以JSON格式而不是x-www-form-urlencoded格式发送数据。
以下JSON格式
params: {
'grant_type': 'authorization_code',
'code': 'my_secret_code
}
可以像这样转换为x-www-form-urlencoded:
params = 'grant_type=authorization_code&code=my_secret_code'
尝试更新您的请求,如下所示:
const params = 'grant_type=authorization_code&code=' + req.body.code
+ '&redirect_uri=' + redirect_uri
+ '&client_id=' + process.env.CLIENT_ID
+ '&client_secret=' + process.env.CLIENT_SECRET';
axios.post(token_uri,
params,
{
headers: {
'Authorization': `Basic ${auth}`,
'Content-Type':'application/x-www-form-urlencoded'
}
})
.then(res=>{
console.log(res.data)
})
.catch(err=>{
console.log(err)
})
Spotify API授权代码流返回'unsupported
我正在尝试使用Node.js应用程序访问Spotify Web API。我已经指定grant_type
为authorization_code
但收到unsupported_grant_type
错误描述grant_type must be client_credentials, authorization_code or refresh_token
。
据我所知,我的post
请求格式正确,值都正确。不知道还有什么要检查。
app.post('/auth', (req, res)=>{
const auth = Buffer
.from(`${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`)
.toString('base64');
axios.post(token_uri, {}, {
params: {
'grant_type': 'authorization_code',
'code': req.body.code,
'redirect_uri': redirect_uri,
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET
}, headers: {
'Authorization': `Basic ${auth}`,
'Content-Type':'application/x-www-form-urlencoded'
}
})
.then(res=>{
console.log(res.data)
})
.catch(err=>{
console.log(err)
})
})
回答如下:
您正确设置了内容类型,但是您要以JSON格式而不是x-www-form-urlencoded格式发送数据。
以下JSON格式
params: {
'grant_type': 'authorization_code',
'code': 'my_secret_code
}
可以像这样转换为x-www-form-urlencoded:
params = 'grant_type=authorization_code&code=my_secret_code'
尝试更新您的请求,如下所示:
const params = 'grant_type=authorization_code&code=' + req.body.code
+ '&redirect_uri=' + redirect_uri
+ '&client_id=' + process.env.CLIENT_ID
+ '&client_secret=' + process.env.CLIENT_SECRET';
axios.post(token_uri,
params,
{
headers: {
'Authorization': `Basic ${auth}`,
'Content-Type':'application/x-www-form-urlencoded'
}
})
.then(res=>{
console.log(res.data)
})
.catch(err=>{
console.log(err)
})