从 spotify 请求访问代码时获取 "malformed or illegal request"

Getting "malformed or illegal request" when requesting access code from spotify

我一直在尝试从 spotify 请求访问代码,以便我可以获得访问令牌并刷新它,但由于某种原因,我收到一条错误消息 "malformed or illegal request"。我认为这可能与 redirect_uri 有关。我完全不确定我应该如何格式化它以及它是否真的很重要。这就是我一直在做的事情:

const authorize_url = "https://accounts.spotify.com/authorize"; 

request({url:authorize_url, form:{client_id: "6...a", response_type: "code", redirect_uri: "http://localhost:8888/callback", scope: "user-modify-playback-state"}}, function(code, error){
   console.log(code);
   console.log(error);
});

这是他们在 api 上的示例("state" 变量是可选的):

GET https://accounts.spotify.com/authorize?client_id=5fe01282e44241328a84e7c5cc169165&response_type=code&redirect_uri=https%3A%2F%2Fexample.com%2Fcallback&scope=user-read-private%20user-read-email&state=34fFs29kd09

错误消息到此结束:

body: '<!DOCTYPE html>\n' +
'<html lang=en>\n' +
'  <meta charset=utf-8>\n' +
'  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">\n' +
'  <title>Error 400 (Bad Request)!!1</title>\n' +
'  <style>\n' +
'    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n' +
'  </style>\n' +
'  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>\n' +
'  <p><b>400.</b> <ins>That’s an error.</ins>\n' +
'  <p>Your client has issued a malformed or illegal request.  <ins>That’s all we know.</ins>\n',

根据 documentation,您应该使用查询参数而不是表单参数发出请求。因此请确保您的请求与您提供的示例格式相同。

感谢@portatlas 解决了问题 这是我所做的:

const propertyObject = {client_id: "6...b", response_type: "code", redirect_uri: "http://localhost:8888", scope: "user-modify-playback-state"};

request.get({url:"https://accounts.spotify.com/authorize", qs:propertyObject}, function(code, error, state){
console.log(code);
console.log(state);
console.log(error);
});