使用 Meteor HTTP 在 Spotify API 上请求 access_token 时不支持的授权类型错误 [英] Unsupported grant type error when requesting access_token on Spotify API with Meteor HTTP
问题描述
在使用 Meteor HTTP 在 Spotify API 上请求 access_token 时,我一直无法解决问题.事实上,当我对 Spotify 进行 POST 调用时 https://accounts.spotify.com/api/token.我得到以下回复:
I've been unable to solve a problem while requesting an access_token on Spotify API with Meteor HTTP. Indeed, when I make a POST call to the Spotify https://accounts.spotify.com/api/token. I get the following response :
{"statusCode":400,"content":"{"error":"unsupported_grant_type","error_description":"grant_type must be client_credentials, authorization_code or refresh_token"}"
我认为这可能与 Content-Type 标头和 BODY 参数的编码有关,但我一直无法解决此问题.我尝试同时使用 data 和 params,但这些都不起作用.
I think this may have something to do with the Content-Type header and the encoding of the BODY parameter but I haven't been able to solve this issue. I tried to use both data and params and none of these worked.
这是我的代码:
HTTP.post("https://accounts.spotify.com/api/token", {
data: {
grant_type : "authorization_code",
code : authCode,
redirect_uri : Router.routes['redirect_spotify'].url()
},
headers: {
'Authorization' : "Basic " + CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse("xxxx:yyyyy")),
'Content-Type':'application/x-www-form-urlencoded'
}
}, function(error, result) {
console.log("POST made with data : %j", result);
if (error){
Registrations.remove({userId : this.userId });
return;
}
Registrations.update({
userId : this.userId },
{$set : {
state: "Done",
accessToken: result.access_token,
//TODO expires
refreshToken: result.refresh_token
}},
{ upsert : true}
);
});
提前谢谢大家:) 爱流星
Thank you all in advance :) Love Meteor
推荐答案
您需要使用 params
而不是 data
.因此,您的代码将是:
You need to use params
instead of data
. Thus, your code would be:
HTTP.post("https://accounts.spotify.com/api/token", {
params: {
grant_type : "authorization_code",
code : authCode,
redirect_uri : Router.routes['redirect_spotify'].url()
},
headers: {
'Authorization' : "Basic " + CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse("xxxx:yyyyy")),
'Content-Type':'application/x-www-form-urlencoded'
}
}, function(error, result) {
...
});
这篇关于使用 Meteor HTTP 在 Spotify API 上请求 access_token 时不支持的授权类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!