【问题标题】:Can't get access token from Spotify API无法从 Spotify API 获取访问令牌
【发布时间】:2018-01-08 07:22:12
【问题描述】:

我正在尝试使用请求模块向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);
  });
};

谁能看到这里可能出了什么问题?我得到的只是一个不起眼的“哎呀!每次出错的错误页面。

【问题讨论】:

    标签: api express request token spotify


    【解决方案1】:

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

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

    【讨论】:

    • 不幸的是,这些文档还提到您可以在 POST 正文中添加 client_id 和 client_secret。将它们作为标题没有任何区别:/
    • @J.Davies 你说得对,跳过那部分。我发现了问题,将编辑我的帖子
    猜你喜欢
    • 2020-10-09
    • 2017-02-14
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 2016-01-06
    • 2020-01-09
    相关资源
    最近更新 更多