无法从Spotify API

时间:2017-08-01 12:18:43

标签: api express request token spotify

我正在尝试使用Request模块向https://accounts.spotify.com/api/token发出POST请求以获取访问令牌。我已经使用我的Spotify开发帐户注册了重定向URI。这是我明确的/redirect路线。

const Request = require('request');

module.exports = (req, res) => {
  const data = {
    grant_type: 'authorization_code',
    code: req.query.code,
    redirect_uri: 'http://localhost:3000/redirect',
    client_id: process.env.SPOTIFY_ID,
    client_secret: process.env.SPOTIFY_SECRET
  }

  const options = {
    method: 'POST',
    url: 'https://accounts.spotify.com/api/token',
    json: true,
    body: data
  }

  Request(options, (error, response, body) => {
    if (error) return console.log(error);
    res.end(body);
  });
};

有谁能看到这里可能出错的地方?我得到的只是一个不起眼的'哎呀!每次都出错了'错误页面。

1 个答案:

答案 0 :(得分:0)

数据参数必须作为表单数据传递:

const options = {
  method: 'POST',
  url: 'https://accounts.spotify.com/api/token',
  json: true,
  form: data
}
相关问题