【问题标题】:Unsupported grant type error when requesting access_token on Spotify API with Meteor HTTP使用 Meteor HTTP 在 Spotify API 上请求 access_token 时出现不支持的授权类型错误
【发布时间】:2026-01-12 13:35:01
【问题描述】:

在使用 Meteor HTTP 在 Spotify API 上请求 access_token 时,我无法解决问题。事实上,当我对 Spotify https://accounts.spotify.com/api/token 进行 POST 调用时。我得到以下回复:

{"statusCode":400,"content":"{\"error\":\"unsupported_grant_type\",\"error_description\":\"grant_type must be client_credentials, authorization_code or refresh_token\"}"

我认为这可能与 Content-Type 标头和 BODY 参数的编码有关,但我无法解决此问题。我尝试同时使用数据和参数,但这些都不起作用。

这是我的代码:

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

提前谢谢大家:)爱流星

【问题讨论】:

  • 我假设您将“authorization_code”替换为您的授权码,对吧?
  • authorization_code 是一个定义授权方案的字符串。授权码在代码中

标签: javascript http meteor http-post spotify


【解决方案1】:

您需要使用params 而不是data。因此,您的代码将是:

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

【讨论】:

  • 这帮助我弄清楚使用query 而不是body 用于Swift Vapor HTTP 帖子。谢谢
【解决方案2】:

当方法为 POST 时,也可以在请求正文中以查询参数格式发送数据。

例如: 将请求正文中的数据值、name = 'Tommy' 和 age = 26 发送为

name=Tommy&age=26

【讨论】:

  • 这解决了我在尝试发送授权信息时遇到的问题
最近更新 更多