我正在尝试建立一个网站,在那里登录的用户可以从他们各自的spotify帐户收听他们的播放列表。我一直在关注web-playback-sdk。
我可以用浏览器连接我的spotify账户。但我希望每个用户都能从他们的spotify账户上收听他们最喜欢的歌曲。为此,我想使用spotify Oauth,但我在阅读文档时感到困惑。有人能帮我解决这个问题吗?
发布于 2020-11-10 00:14:57
这里有两个函数可以帮助您使用Implicit Grant Flow初始化Spotify Web Playback SDK,以获得临时的OAuth令牌。如果您想要可刷新的用户授权,则需要使用Authorization Code Flow。
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
https://stackoverflow.com/questions/62484641
复制相似问题