我一直在尝试在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
效果很好。
发布于 2014-12-19 05:40:26
我也遇到过类似的问题(但在Java中)。类似的解决方案是
headers: {
'Content-Type':'application/x-www-form-urlencoded'
}发布于 2015-01-27 18:16:43
在发送JSON对象时,需要使用params而不是data。相关问题:Unsupported grant type error when requesting access_token on Spotify API with Meteor HTTP
发布于 2021-05-12 05:16:08
我已经使用下面的函数成功地尝试从Spotify获取访问令牌。正如您所看到的,您不需要指定Content-Type,只需要使用params而不是data (就axios而言)。还要确保首先将客户端id和客户端密钥组合在一起,并在它们之间加上一个":“,然后将组合后的字符串转换为base64。
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);
})
}https://stackoverflow.com/questions/25432678
复制相似问题