【发布时间】:2019-08-22 22:47:50
【问题描述】:
在 Express 后端服务器上使用 axios 发出发布请求时,我试图从 Spotify API 检索访问令牌。到目前为止,我一直没有成功。我收到以下错误:
数据: {错误:'unsupported_grant_type', 错误描述: 'grant_type 必须是 client_credentials、authorization_code 或 refresh_token' } } }
我已经尝试将“grant_type”的“data”属性更改为“params”,但仍然无法正常工作。任何建议都会有所帮助。
const express = require('express');
const axios = require('axios');
const dotenv = require('dotenv');
dotenv.config();
const app = express();
const port = 3000;
const client_id = process.env.CLIENT_ID;
const client_secret = process.env.CLIENT_SECRET;
app.get('/spotify-authorization', (req, res) => {
axios({
method: 'post',
url: 'https://accounts.spotify.com/api/token',
data: {
grant_type: 'client_credentials'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization:
'Basic ' +
Buffer.from(client_id + ':' + client_secret).toString('base64')
}
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
res.send('successful response received!');
});
app.listen(port, () => console.log(`Express app listening on port ${port}!`));
我希望能够在 Spotify API 的响应中检索访问令牌。请帮忙!
【问题讨论】:
-
我已经尝试过使用“param”和“params”,但不幸的是收到了同样的错误。
-
来自文档:
grant_type Required. As defined in the OAuth 2.0 specification, this field must contain the value "authorization_code".将grant_type: 'client_credentials'更改为grant_type: "authorization_code" -
根据 Spotify API 文档上的客户端凭据流程,grant_type 必须是“client_credentials”。
-
是的,这就是链接。我正在查看客户端凭据流。