1+ <!DOCTYPE html>
2+ < html >
3+ < head >
4+ < title > Spotify Dev Player</ title >
5+ < style >
6+ body {
7+ font-family : Arial, sans-serif;
8+ max-width : 800px ;
9+ margin : 2rem auto;
10+ padding : 0 1rem ;
11+ }
12+ .credentials {
13+ display : flex;
14+ flex-direction : column;
15+ gap : 1rem ;
16+ margin-bottom : 2rem ;
17+ }
18+ input {
19+ padding : 0.5rem ;
20+ width : 100% ;
21+ }
22+ button {
23+ padding : 0.5rem 1rem ;
24+ cursor : pointer;
25+ }
26+ .hidden {
27+ display : none;
28+ }
29+ </ style >
30+ </ head >
31+ < body >
32+ < div id ="setup " class ="credentials ">
33+ < input type ="text " id ="clientId " placeholder ="Enter Spotify Client ID ">
34+ < input type ="text " id ="clientSecret " placeholder ="Enter Spotify Client Secret ">
35+ < button onclick ="startAuth() "> Initialize Player</ button >
36+ </ div >
37+
38+ < div id ="player " class ="hidden ">
39+ < button onclick ="togglePlay() "> Play/Pause</ button >
40+ </ div >
41+
42+ < script src ="https://sdk.scdn.co/spotify-player.js "> </ script >
43+ < script >
44+ let playerInstance = null ;
45+ const redirectUri = 'http://gptgames.dev/tools/spotify_test.html' ;
46+
47+ async function startAuth ( ) {
48+ const clientId = document . getElementById ( 'clientId' ) . value ;
49+ const state = Math . random ( ) . toString ( 36 ) . substring ( 7 ) ;
50+ const scope = 'streaming user-read-email user-read-private' ;
51+
52+ const authUrl = `https://accounts.spotify.com/authorize?client_id=${ clientId } &response_type=code&redirect_uri=${ encodeURIComponent ( redirectUri ) } &scope=${ encodeURIComponent ( scope ) } &state=${ state } ` ;
53+ window . location . href = authUrl ;
54+ }
55+
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+ }
81+ }
82+
83+ function initializePlayer ( token ) {
84+ document . getElementById ( 'setup' ) . classList . add ( 'hidden' ) ;
85+ document . getElementById ( 'player' ) . classList . remove ( 'hidden' ) ;
86+
87+ window . onSpotifyWebPlaybackSDKReady = ( ) => {
88+ playerInstance = new Spotify . Player ( {
89+ name : 'Development Player' ,
90+ getOAuthToken : cb => { cb ( token ) ; } ,
91+ volume : 0.5
92+ } ) ;
93+
94+ playerInstance . addListener ( 'ready' , ( { device_id } ) => {
95+ console . log ( 'Ready with Device ID' , device_id ) ;
96+ } ) ;
97+
98+ playerInstance . connect ( ) ;
99+ } ;
100+ }
101+
102+ function togglePlay ( ) {
103+ if ( playerInstance ) {
104+ playerInstance . togglePlay ( ) ;
105+ }
106+ }
107+
108+ if ( window . location . search . includes ( 'code=' ) ) {
109+ handleCallback ( ) ;
110+ }
111+ </ script >
112+ </ body >
113+ </ html >
0 commit comments