Spotify API错误。缺少401个权限。尝试播放曲目时 [英] Spotify API Error. 401 Permissions missing. When Attempting to Play Track

查看:25
本文介绍了Spotify API错误。缺少401个权限。尝试播放曲目时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用spotify-web-api-node在我的应用程序上播放曲目

  const playSong = async () => {
    // Verify access token with
    console.log(spotifyApi.getAccessToken())

    setCurrentTrackId(track.track.id);
    setIsPlaying(true);
    spotifyApi
      .play({
        uris: [track.track.uri],
      })
      .then((d) => console.log("hi", d))
      .catch((e) => console.log("err", e));
  };

https://github.com/caseysiebel/spotify-clone/blob/main/components/Song.tsx#L19

当我发出对spotifyApi.play()的此调用时,我将收到此错误,错误代码为401。

我在我的应用程序中设置了访问令牌和刷新令牌。我能够登录并从API获取数据,如用户、播放列表和曲目数据。但当我尝试播放曲目时,提示我未经许可。

我以为这可能与我在Spotify API中设置scopes的方式有关,但它们在我看来是正确的。

const scopes = [
  "user-read-email",
  "playlist-read-private",
  "playlist-read-collaborative",
  "user-read-email",
  "streaming",
  "user-read-private",
  "user-library-read",
  "user-top-read",
  // "user-library-modify"
  "user-read-playback-state",
  "user-modify-playback-state",
  "user-read-currently-playing",
  "user-read-recently-played",
  "user-read-playback-state",
  "user-follow-read",
].join(",");

https://github.com/caseysiebel/spotify-clone/blob/main/lib/spotify.ts#L5

这似乎与传入授权LOGIN_URLscopes有关,但streaming作用域确实存在,我看不出有什么问题。看起来是这样的,因为某些API请求可以工作,而其他API请求说他们没有权限。

有人看到导致此问题的原因了吗?缺少权限问题和Spotify API?

推荐答案

第23行lib/spotify.ts需要scope: scopes,而非scopes: scopes,

在这里实际使用类型检查可能会有所帮助。

https://github.com/currenthandle/spotify-clone/commit/736108de1a1a132c43810d8415584f3be198609a#diff-b030a1ce426906990f8ed48fd8be76f54558b94141f4c811e679fef6661d396dR23

这篇关于Spotify API错误。缺少401个权限。尝试播放曲目时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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