我一直在尝试在MeteorJS中重新创建spotify oauth连接。我已经获得了请求访问和刷新令牌,但我现在仍然收到415错误。以下是相关代码:
var results = HTTP.post(
'https://accounts.spotify.com/api/token',
{
data: {
code: code,
redirect_uri: redirectURI,
grant_type: 'authorization_code',
client_id: clientID,
client_secret: clientSecret
},
headers: {
'Content-Type':'application/json'
}
}
);
我似乎无法在此演示中找到问题和代码的任何其他良好文档:
https://github.com/spotify/web-api-auth-examples/tree/master/authorization_code
完美无缺。
答案 0 :(得分:11)
我有类似的问题(但在Java中)。类似的解决方案是
headers: {
'Content-Type':'application/x-www-form-urlencoded'
}
答案 1 :(得分:2)
发送JSON对象时,需要使用params
而不是data
。相关问题:Unsupported grant type error when requesting access_token on Spotify API with Meteor HTTP
答案 2 :(得分:1)
我已成功尝试使用以下函数从 Spotify 获取访问令牌。如您所见,您不需要指定 Content-Type,而只需要使用 params 而不是 data(就 axios 而言)。还要确保首先将客户端 ID 和客户端密钥与它们之间的“:”组合起来,然后将组合后的字符串转换为 base 64。
let getAccessToken = () => {
let options = {
url: 'https://accounts.spotify.com/api/token',
method: 'POST',
headers: {
// 'Content-Type':'application/x-www-form-urlencoded',
'Authorization': `Basic <base64 encoded client_id:client_secret>`
},
params: {
grant_type: 'client_credentials'
}
}
axios(options)
.then((resp) => {
console.log('resp', resp.data)
})
.catch((err) => {
console.log('ERR GETTING SPOTIFY ACCESS TOKEN', err);
})
}
答案 3 :(得分:-2)
如果您正在执行此操作,则无法使用,因为same origin policy不允许您从客户端发布到其他域。
如果这是服务器端,我建议使用预先存在的spotify api npm模块,而不是编写自己的请求。 npmjs.org上有很多spotify api实现。
使用arunoda的npm package在流星应用程序中集成npm包