Spotify 白名单 URI 仍然返回 { "error": "invalid_grant", "error_description": "Invalid redirect URI" }

let*_*gin 6 javascript spotify express

我知道已经有类似的问题,但所有的答案大多是“哦,我忘了把斜线放在最后”但这绝对让我发疯。我试图从 Spotify API 获取访问令牌,但我不断收到无效的重定向 uri 错误。

这是我的 api 调用

const request = require('superagent');

const data = {
    grant_type: 'authorization_code',
    code: code,
   // redirect_uri: encodeURIComponent('http://localhost:3000/Test')
   redirect_uri: 'http://localhost:3000/Test'
};

request.post('https://accounts.spotify.com/api/token')
    .set({
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic ' + base64.encode(configs.client_id + ':' + configs.client_secret)
    })
    .send(data)
    .end((err, tokenRes) => {
        if (tokenRes) {
            res.send({token: tokenRes})
        } else {
            res.error(err);
        }
    });
Run Code Online (Sandbox Code Playgroud)

这些是我列入白名单的 URI:

http://localhost:3000/LoginRedirect

http://localhost:3000/测试

http://localhost:3000/首页

我在最后用斜杠在白名单中添加了很多组合,删除了 http:// s 搜索通配符,但我无法摆脱这个错误......感谢任何帮助。

小智 7

经过一些重构后,我自己也遇到了同样的问题,我自己也快疯了。所述redirect_uri在后请求必须是一样的第一redirect_uri从客户端。来自Spotify 文档

必需的。此参数仅用于验证(没有实际重定向)。此参数的值必须与请求授权码时提供的 redirect_uri 值完全匹配。