415从请求令牌Spotify API返回

时间:2018-02-23 12:10:03

标签: api spotify axios

我试图从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
};

4 个答案:

答案 0 :(得分:2)

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': '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) {
});

答案 1 :(得分:1)

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

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');
});

答案 2 :(得分:0)

有效!!! 我做的是:   - 更改'application / x-www-form-urlencoded'的内容类型   - client_id和client_secret从标题中获取并在body中的grant_type之前发布   - 将'数据'更改为'params'

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 :(得分:0)

如果从客户端(浏览器)进行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));
}
相关问题