我正在尝试发出POST请求,但在我的Chrome网络标签中收到以下消息:
{错误:“ unsupported_grant_type”,…}错误:“ unsupported_grant_type” error_description:“ grant_type必须为client_credentials, 授权码或刷新令牌”
我一直在使用Axios进行RESTful通话,这是POST请求:
async componentDidMount() {
const encodedString = 'blah'//some encoded string
const [initSpotResponse] = await Promise.all([
axios.post('https://accounts.spotify.com/api/token',
{ data: { grant_type: 'client_credentials' } },
{
headers: {
'Authorization': `Basic ${encodedString}`,
'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
}
)
]);
}
我已经尝试了其他StackOverflow帖子中提到的各种方法,但是似乎没有任何效果。有没有人有创建这样的POST请求的经验?我还没有看到有关此问题的特定于axios的帖子-我应该放弃axios(如果是的话,我应该切换到什么位置?)?
答案 0 :(得分:-1)
axios.post
接受数据对象(axios.post(url[, data[, config]])
)作为第二个参数,用qs
序列化数据并尝试进行此操作,如here所述:
const qs = require('querystring');
async componentDidMount() {
const encodedString = 'blah'//some encoded string
const [initSpotResponse] = await Promise.all([
axios.post('https://accounts.spotify.com/api/token',
qs.stringify({ grant_type: 'client_credentials' }),
{
headers: {
'Authorization': `Basic ${encodedString}`,
'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
}
)
]);
}
答案 1 :(得分:-1)
尝试
async componentDidMount() {
let data = JSON.stringify({
grant_type: 'client_credentials'
})
const encodedString = 'blah'//some encoded string
const [initSpotResponse] = await Promise.all([
axios.post('https://accounts.spotify.com/api/token',
data,
{
headers: {
'Authorization': `Basic ${encodedString}`,
'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
}
)
]);
}