415从请求令牌Spotify API返回
我正在尝试从Spotify api接收令牌。不幸的是,我一直收到415。你能帮我一下,让我知道我做错了什么吗415从请求令牌Spotify API返回,api,spotify,axios,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,
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
};
415
错误代码与错误的内容类型或内容编码相关,()
我不知道axios,但请看一下spotify github上的示例
根据github()上的这个问题,尝试使用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) {
});
415
错误代码与错误的内容类型或内容编码相关,()
我不知道axios,但请看一下spotify github上的示例
根据github()上的这个问题,尝试使用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) {
});
它工作!!!
我所做的是:
-更改“application/x-www-form-urlencoded”的内容类型
-客户端id和客户端机密从标题中获取,并在正文中授予类型之前发布
-将“数据”更改为“参数”
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/它可以工作!!!
我所做的是:
-更改“application/x-www-form-urlencoded”的内容类型
-客户端id和客户端机密从标题中获取,并在正文中授予类型之前发布
-将“数据”更改为“参数”
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/在花了一个小时试图弄清楚如何获得令牌后,我想出了这个答案!:)
在花了一个小时试图弄清楚如何获得代币后,我想出了这个答案!:)
如果从客户端(浏览器)进行API调用,请尝试以下解决方案:
getTokken(){
常数urlSpotify=”https://accounts.spotify.com/api/token";
axios({
方法:“张贴”,
url:urlSpotify,
数据:“授予\类型=客户\凭证”,
标题:{
接受:“应用程序/json”,
“内容类型”:“应用程序/x-www-form-urlencoded”,
},
认证:{
用户名:process.env.REACT\u APP\u SPTID\u KEY,//用户ID
密码:process.env.REACT\u APP\u SPCS\u KEY,//用户密码
},
})
。然后((响应)=>{
控制台日志(响应);
})
.catch((err)=>console.log(err));
}
如果从客户端(浏览器)调用API,请尝试以下解决方案:
getTokken(){
常数urlSpotify=”https://accounts.spotify.com/api/token";
axios({
方法:“张贴”,
url:urlSpotify,
数据:“授予\类型=客户\凭证”,
标题:{
接受:“应用程序/json”,
“内容类型”:“应用程序/x-www-form-urlencoded”,
},
认证:{
用户名:process.env.REACT\u APP\u SPTID\u KEY,//用户ID
密码:process.env.REACT\u APP\u SPCS\u KEY,//用户密码
},
})
。然后((响应)=>{
控制台日志(响应);
})
.catch((err)=>console.log(err));
}
感谢您的反馈。我还尝试了request promise并检查了github示例,但不幸的是,我在respose主体中仍然收到相同的结果(415)是否还有其他错误消息?statusText:“Unported Media Type”,数据:{error:“server_error”,error_description:“意外状态:415”}感谢您的反馈。我还尝试了request promise并检查了github示例,但不幸的是,我仍然在respose正文中收到相同的结果(415)是否还有其他错误消息?statusText:“Unported Media Type”,数据:{error:“server_error”,error_description:“意外状态:415”}