首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spotify API Oauth

Spotify API Oauth
EN

Stack Overflow用户
提问于 2020-06-20 18:34:15
回答 1查看 63关注 0票数 0

我正在尝试建立一个网站,在那里登录的用户可以从他们各自的spotify帐户收听他们的播放列表。我一直在关注web-playback-sdk

我可以用浏览器连接我的spotify账户。但我希望每个用户都能从他们的spotify账户上收听他们最喜欢的歌曲。为此,我想使用spotify Oauth,但我在阅读文档时感到困惑。有人能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2020-11-10 00:14:57

这里有两个函数可以帮助您使用Implicit Grant Flow初始化Spotify Web Playback SDK,以获得临时的OAuth令牌。如果您想要可刷新的用户授权,则需要使用Authorization Code Flow

代码语言:javascript
复制
function logInWithSpotify() {
  const clientId = '<insert-client-id>'; // Get ClientID from https://developer.spotify.com/dashboard/
  const redirectUrl = '<insert-redirect-uri>'; // Add redirect URIs to the whitelist in the settings https://developer.spotify.com/dashboard/
  const scopes = ['streaming', 'user-read-email', 'user-read-private']; // Required scopes

  // Implicit Grant Authorization Flow
  // More info: https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow
  window.location = [
    'https://accounts.spotify.com/authorize',
    `?client_id=${clientId}`,
    `&scope=${encodeURIComponent(scopes.join(' '))}`,
    `&redirect_uri=${encodeURIComponent(redirectUrl)}`,
    '&response_type=token',
    '&show_dialog=true',
  ].join('');
}

function getOAuthToken(cb) {
  const hash = window.location.hash
    .substring(1)
    .split('&')
    .reduce((values, item) => {
      if (item) {
        const [key, value] = item.split('=');
        values[key] = decodeURIComponent(value);
      }
      return values;
    }, {});
  // Clear the hash
  window.location.hash = '';

  const accessToken = hash.access_token;
  if (!accessToken) logInWithSpotify();
  cb(accessToken);
}

const player = new Spotify.Player({
  name: 'Web Playback SDK Quick Start Player',
  getOAuthToken: getOAuthToken,
});

下面是如何使用Spotify Web Playback SDK的更详细的要点:https://gist.github.com/lundgren2/4a5c7a1ba7916bbcf460901620166ab2

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62484641

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档