我一直试图从spotify请求一个访问代码,这样我就可以获得一个访问令牌并刷新它,但由于某种原因,我收到了一个错误,说“格式错误或非法请求”。我想可能和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',发布于 2020-04-09 02:16:50
根据documentation,您应该使用查询参数而不是表单参数发出请求。因此,请确保您的请求与您提供的示例的格式相同。
发布于 2020-04-10 04:34:20
多亏了@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);
});https://stackoverflow.com/questions/61084133
复制相似问题