【问题标题】:Spotify API Authorization Code Flow Returning 'unsupported_grant_type' ErrorSpotify API 授权代码流返回“unsupported_grant_type”错误
【发布时间】:2018-09-22 21:07:57
【问题描述】:

我正在尝试使用 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)
    })
})

【问题讨论】:

    标签: node.js authentication spotify


    【解决方案1】:

    您正确设置了内容类型,但您以 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)
        })
    

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 2019-01-25
      • 2019-05-03
      • 2021-04-02
      • 2018-08-12
      • 2017-12-23
      • 1970-01-01
      • 2022-08-08
      • 2018-05-21
      相关资源
      最近更新 更多