|
31 | 31 | <body> |
32 | 32 | <div id="setup" class="credentials"> |
33 | 33 | <input type="text" id="clientId" placeholder="Enter Spotify Client ID"> |
34 | | - <input type="text" id="clientSecret" placeholder="Enter Spotify Client Secret"> |
35 | 34 | <button onclick="startAuth()">Initialize Player</button> |
36 | 35 | </div> |
37 | 36 |
|
|
49 | 48 | const state = Math.random().toString(36).substring(7); |
50 | 49 | const scope = 'streaming user-read-email user-read-private'; |
51 | 50 |
|
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 | + |
53 | 58 | window.location.href = authUrl; |
54 | 59 | } |
55 | 60 |
|
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; |
81 | 72 | } |
82 | 73 |
|
83 | 74 | function initializePlayer(token) { |
84 | 75 | document.getElementById('setup').classList.add('hidden'); |
85 | 76 | document.getElementById('player').classList.remove('hidden'); |
86 | 77 |
|
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 | + }); |
93 | 83 |
|
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 | + }); |
97 | 87 |
|
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(); |
100 | 99 | } |
101 | 100 |
|
102 | 101 | function togglePlay() { |
|
105 | 104 | } |
106 | 105 | } |
107 | 106 |
|
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 | + } |
110 | 113 | } |
111 | 114 | </script> |
112 | 115 | </body> |
|
0 commit comments