【问题标题】:415 coming back from requesting a token Spotify API415 从请求令牌 Spotify API 中返回
【发布时间】:2018-02-23 12:10:03
【问题描述】:

我正在尝试从 Spotify api 接收令牌。不幸的是,我一直收到 415。你能帮我,让我知道我做错了什么吗?

const axios = require('axios');

const getToken = (code) => {
    return axios({
        method: 'post',
        url:'https://accounts.spotify.com/api/token',
        form: {
            code,
            grant_type :'authorization_code',
            redirect_uri: process.env.SPOTIFY_REDIRECT
        },
        headers: {
            'Authorization': 'Basic ' + (new Buffer(process.env.SPOTIFY_ID + ':' + process.env.SPOTIFY_SECRET).toString('base64')),
            'Content-Type': 'application/json'
        }
    }).then(token => {
        return token;
    }).catch(e=> {
        console.log(e);
        return e.response;
    });
};

module.exports = {
    getToken
};

【问题讨论】:

    标签: api spotify axios


    【解决方案1】:

    415 错误代码与错误的内容类型或内容编码有关,(https://httpstatuses.com/415)

    我不知道axios,但请看一下Spotify github上的示例https://github.com/spotify/web-api-auth-examples/blob/master/authorization_code/app.js#L74

    根据github上的这个问题(https://github.com/spotify/web-api/issues/321),尝试使用content-type 'Content-Type': 'application/x-www-form-urlencoded'

    有axios的例子

    axios({
        url: "https://accounts.spotify.com/api/token",
        method: "post",
        params: {
            grant_type: "client_credentials"
        },
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/x-www-form-urlencoded"
        },
        auth: {
            username: "YOUR-CLIENT-ID",
            password: "YOUR-CLIENT-SECRET"
        }
    }).then(function (response) {
        console.log(response);
    }).catch(function (error) {
    });
    

    【讨论】:

    • 感谢您的反馈。我也尝试过 request-promise 并检查了 github 示例,但不幸的是我仍然收到相同的结果(415)
    • 在respose正文中是否还有其他错误信息?
    • statusText:'不支持的媒体类型',数据:{错误:'server_error',error_description:'意外状态:415'}
    【解决方案2】:

    有效!!! 我所做的是: - 更改“应用程序/x-www-form-urlencoded”的内容类型 - client_id 和 client_secret 取自标头并在正文中的 grant_type 之前发布 - 将“数据”更改为“参数”

    const axios = require('axios');
    
    const getToken = (code) => {
        return axios({
            method: 'post',
            url:'https://accounts.spotify.com/api/token',
            params: {
                client_id: process.env.SPOTIFY_ID,
                client_secret: process.env.SPOTIFY_SECRET,
                code,
                grant_type :'authorization_code',
                redirect_uri: process.env.SPOTIFY_REDIRECT
            },
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(token => {
            return token;
        }).catch(e=> {
            return e.response.data;
        });
    };
    

    它产生了一个漂亮的令牌 \m/

    【讨论】:

      【解决方案3】:

      花了一个小时试图弄清楚如何获得令牌后,我想出了这个答案! :)

      const axios = require('axios');
      const express = require('express');
      const app = express();
      
      const client_id= 'YOURCLIENTID';
      const client_secret = 'YOURCLIENTSECRET';
      
      app.get('/api/token', (req, res) => {
        axios({
          method: 'post',
          url: 'https://accounts.spotify.com/api/token',
          headers: {
          'Authorization': 'Basic ' + (new Buffer.from(client_id + ':' + client_secret).toString('base64')),        
          'Content-Type': 'application/x-www-form-urlencoded'
          },
          params: {
            grant_type: 'client_credentials'
          },
          json: true,
        })
          .then(body => {
            res.send(body.data.access_token);
          })
          .catch(e => {
            console.log(e.response.data);
          });
      });
      
      app.listen(3000, () => {
        console.log('Server Listening on port 3000');
      });
      

      【讨论】:

        【解决方案4】:

        如果您从客户端(浏览器)进行 API 调用,请尝试以下解决方案:

        getTokken() {
        const urlSpotify = "https://accounts.spotify.com/api/token";
        axios({
          method: "post",
          url: urlSpotify,
          data: "grant_type=client_credentials",
          headers: {
            Accept: "application/json",
            "Content-Type": "application/x-www-form-urlencoded",
          },
          auth: {
            username: process.env.REACT_APP_SPTID_KEY, // User ID
            password: process.env.REACT_APP_SPCS_KEY,  // User Secret
          },
        })
          .then((response) => {
            console.log(response);
          })
          .catch((err) => console.log(err));
        }
        

        【讨论】:

          猜你喜欢
          • 2018-02-18
          • 2018-12-02
          • 2017-11-22
          • 2019-08-21
          • 2015-05-10
          • 2012-08-19
          • 2021-08-08
          • 2016-11-02
          • 2020-01-04
          相关资源
          最近更新 更多