Spotify API错误400:"仅支持有效的承载身份验证" [英] Spotify API error 400: "only valid bearer authentication supported"

查看:18
本文介绍了Spotify API错误400:"仅支持有效的承载身份验证"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用API尝试通过访问令牌获取我个人资料的数据。我使用了网站中给出的授权示例代码中的给定代码,并且我使用了

        let parsed = queryString.parse(window.location.search);
        let accessToken = parsed.access_token;
        console.log(parsed);
        console.log(accessToken);
        fetch('https://api.spotify.com/v1/me', {
            headers: {'Authorization': 'Bearer' + accessToken},
            json: true
        }).then((res) => res.json())
        .then(data => console.log(data));
    }

但是,即使访问令牌正常工作,仍会出现400错误。我甚至签入了该特定函数的Try It方法,并且使用相同的标记,它能够获得数据。有关我在用于身份验证的示例代码中所做更改的相关信息,我更改了reDirect_uri="Localhost:8888/Callback",然后也更改了

res.redirect('http://localhost:3000/profile/?' +
          querystring.stringify({
            access_token: access_token,
            refresh_token: refresh_token
          }));
      }

转到我的应用程序的个人资料页面。我想知道我是否需要允许我的个人资料页面,但我已经添加了它,以重定向URL以防万一。 感谢阅读

推荐答案

"持有者"和您的令牌之间必须有空格。这就是缺少的东西。

let parsed = queryString.parse(window.location.search);
    let accessToken = parsed.access_token;
    console.log(parsed);
    console.log(accessToken);
    fetch('https://api.spotify.com/v1/me', {
        headers: {'Authorization': 'Bearer ' + accessToken},
        json: true
    }).then((res) => res.json())
    .then(data => console.log(data));
}

注意我是如何在"持有者"之后添加空格的。

在Spotify的网站上也有他们的示例代码。 https://developer.spotify.com/documentation/general/guides/authorization-guide/ (没有永久链接,只需在此页面上搜索"持有者")

另外,我建议您使用模板文字来定义这样的字符串,因为它更容易、更具可读性,并且可以防止将来出现此类错误。

headers: {'Authorization': `Bearer ${accessToken}`}
您可以在此处阅读有关此内容的更多信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

这篇关于Spotify API错误400:"仅支持有效的承载身份验证"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆