Skip to content

Commit c4d14ae

Browse files
committed
- playing around with the spotify API
1 parent 3ea6bc8 commit c4d14ae

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

tools/spotify_test.html

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
<body>
3232
<div id="setup" class="credentials">
3333
<input type="text" id="clientId" placeholder="Enter Spotify Client ID">
34-
<input type="text" id="clientSecret" placeholder="Enter Spotify Client Secret">
3534
<button onclick="startAuth()">Initialize Player</button>
3635
</div>
3736

@@ -49,54 +48,54 @@
4948
const state = Math.random().toString(36).substring(7);
5049
const scope = 'streaming user-read-email user-read-private';
5150

52-
const authUrl = `https://accounts.spotify.com/authorize?client_id=${clientId}&response_type=code&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}&state=${state}`;
51+
// Using Implicit Grant Flow instead of Authorization Code Flow
52+
const authUrl = `https://accounts.spotify.com/authorize?client_id=${clientId}` +
53+
`&response_type=token` +
54+
`&redirect_uri=${encodeURIComponent(redirectUri)}` +
55+
`&scope=${encodeURIComponent(scope)}` +
56+
`&state=${state}`;
57+
5358
window.location.href = authUrl;
5459
}
5560

56-
async function handleCallback() {
57-
const urlParams = new URLSearchParams(window.location.search);
58-
const code = urlParams.get('code');
59-
60-
if (code) {
61-
const clientId = document.getElementById('clientId').value;
62-
const clientSecret = document.getElementById('clientSecret').value;
63-
64-
const response = await fetch('https://accounts.spotify.com/api/token', {
65-
method: 'POST',
66-
headers: {
67-
'Content-Type': 'application/x-www-form-urlencoded',
68-
'Authorization': 'Basic ' + btoa(clientId + ':' + clientSecret)
69-
},
70-
body: new URLSearchParams({
71-
code: code,
72-
redirect_uri: redirectUri,
73-
grant_type: 'authorization_code',
74-
client_id: clientId
75-
})
76-
});
77-
78-
const data = await response.json();
79-
initializePlayer(data.access_token);
80-
}
61+
function getHashParams() {
62+
const hashParams = {};
63+
const hash = window.location.hash.substring(1);
64+
const params = hash.split('&');
65+
66+
params.forEach(param => {
67+
const [key, value] = param.split('=');
68+
hashParams[key] = decodeURIComponent(value);
69+
});
70+
71+
return hashParams;
8172
}
8273

8374
function initializePlayer(token) {
8475
document.getElementById('setup').classList.add('hidden');
8576
document.getElementById('player').classList.remove('hidden');
8677

87-
window.onSpotifyWebPlaybackSDKReady = () => {
88-
playerInstance = new Spotify.Player({
89-
name: 'Development Player',
90-
getOAuthToken: cb => { cb(token); },
91-
volume: 0.5
92-
});
78+
playerInstance = new Spotify.Player({
79+
name: 'Development Player',
80+
getOAuthToken: cb => { cb(token); },
81+
volume: 0.5
82+
});
9383

94-
playerInstance.addListener('ready', ({ device_id }) => {
95-
console.log('Ready with Device ID', device_id);
96-
});
84+
playerInstance.addListener('ready', ({ device_id }) => {
85+
console.log('Ready with Device ID', device_id);
86+
});
9787

98-
playerInstance.connect();
99-
};
88+
playerInstance.addListener('initialization_error', ({ message }) => {
89+
console.error('Failed to initialize', message);
90+
});
91+
92+
playerInstance.addListener('authentication_error', ({ message }) => {
93+
console.error('Failed to authenticate', message);
94+
document.getElementById('setup').classList.remove('hidden');
95+
document.getElementById('player').classList.add('hidden');
96+
});
97+
98+
playerInstance.connect();
10099
}
101100

102101
function togglePlay() {
@@ -105,8 +104,12 @@
105104
}
106105
}
107106

108-
if (window.location.search.includes('code=')) {
109-
handleCallback();
107+
// Check if we're returning from auth
108+
if (window.location.hash) {
109+
const params = getHashParams();
110+
if (params.access_token) {
111+
initializePlayer(params.access_token);
112+
}
110113
}
111114
</script>
112115
</body>

0 commit comments

Comments
 (0)