@@ -28,31 +28,35 @@ export class SpotifyAPI {
2828 }
2929
3030 async addToQueue ( tokenPair : TokenPair , trackURI : string , refreshTokenOnFailure : boolean = true ) {
31- logger . verbose ( "Request to add to Queue started." )
32- this . updateTokenIfExpiringSoon ( tokenPair ) . then ( token => {
31+ try {
32+ logger . verbose ( "Request to add to Queue started." )
33+ let token = await this . updateTokenIfExpiringSoon ( tokenPair ) ;
3334 let options = this . createPostSongOptions ( trackURI , token . accessToken ) ;
35+
3436 request ( options , ( res ) => {
3537 res . on ( "end" , ( ) => {
3638 if ( res . statusCode !== 304 ) {
3739 logger . debug ( "Add to queue request failed with code " + res . statusCode ) ;
3840 throw new Error ( "Status Code not 304" ) ;
41+ } else {
42+ logger . debug ( "Add to queue request finished with code " + res . statusCode ) ;
3943 }
4044 } )
4145 } ) . end ( ) ;
42- } ) . catch ( err => {
46+ } catch ( err ) {
4347 if ( refreshTokenOnFailure ) {
4448 logger . verbose ( "Attempting to refresh token after addToQueue api failure." )
45- this . updateToken ( tokenPair ) . then ( newToken => {
46- this . addToQueue ( newToken , trackURI , false )
47- } ) . catch ( err => { throw err } ) ;
49+ let newToken = await this . updateToken ( tokenPair )
50+ await this . addToQueue ( newToken , trackURI , false )
4851 }
4952 throw err ;
50- } )
53+ }
5154 }
5255
5356 async updateTokenIfExpiringSoon ( tokenPair : TokenPair ) : Promise < TokenPair > {
5457 let now = new Date ( ) ;
5558 if ( ( tokenPair . expirationTime . getTime ( ) - now . getTime ( ) ) / 1000 < this . minimumTokenTimeRemaining ) {
59+ logger . verbose ( "Updated token as it was expiring soon or was already expired." ) ;
5660 return this . updateToken ( tokenPair ) ;
5761 }
5862 return tokenPair ;
@@ -70,6 +74,7 @@ export class SpotifyAPI {
7074 try {
7175 let code = await this . repository . getRequestCodeById ( requestId ) ;
7276 let tokenPair = await this . fetchTokenFromCode ( code ) ;
77+ logger . debug ( "Fetched token pair with expiration time " + tokenPair . expirationTime )
7378 this . repository . finishCodeRequest ( requestId , tokenPair ) ;
7479 } catch ( err ) {
7580 logger . error ( "Error while attempting to fetch token pair from code (requestId: " + requestId + ")" ) ;
@@ -80,28 +85,28 @@ export class SpotifyAPI {
8085 private fetchTokenFromRefresh ( tokenPair : TokenPair ) : Promise < TokenPair > {
8186 logger . verbose ( "Attempting to fetch token pair from refresh token" )
8287 let options = this . createRefreshTokenOptions ( ) ;
83- let body = `{' grant_type' : ' refresh_token', ' refresh_token':' ${ tokenPair . refreshToken } ', ' redirect_uri':' ${ encodeURIComponent ( this . redirectUri ) } ', ' client_id: ${ this . clientId } , ' client_secret: ${ this . clientSecret } ' }` ;
84- return this . fetchTokenPair ( options , body ) ;
88+ let body = `grant_type= refresh_token& refresh_token= ${ tokenPair . refreshToken } & redirect_uri= ${ encodeURIComponent ( this . redirectUri ) } & client_id= ${ this . clientId } & client_secret= ${ this . clientSecret } ` ;
89+ return this . fetchTokenPair ( options , body , tokenPair ) ;
8590 }
8691
8792 private fetchTokenFromCode ( code : string ) : Promise < TokenPair > {
8893 let options = this . createFetchFromCodeOptions ( ) ;
89- let body = `{' grant_type' : ' authorization_code', ' code' : ' ${ code } ', ' redirect_uri':' ${ encodeURIComponent ( this . redirectUri ) } , ' client_id: ${ this . clientId } , ' client_secret: ${ this . clientSecret } ' }`
94+ let body = `grant_type= authorization_code& code= ${ code } & redirect_uri= ${ encodeURIComponent ( this . redirectUri ) } & client_id= ${ this . clientId } & client_secret= ${ this . clientSecret } `
9095 return this . fetchTokenPair ( options , body ) ;
9196 }
9297
93- private fetchTokenPair ( requestOptions : RequestOptions , body : string ) : Promise < TokenPair > {
98+ private fetchTokenPair ( requestOptions : RequestOptions , body : string , oldToken ? : TokenPair ) : Promise < TokenPair > {
9499 return new Promise ( ( resolve , reject ) => {
95100 let data = ""
96- logger . debug ( "Making spotify api request:\nBody: " + body + "\nOptions: " + JSON . stringify ( requestOptions ) )
101+ logger . debug ( "Making spotify api token fetch request" ) ;
97102 request ( requestOptions , ( res ) => {
98103 res . on ( 'data' , ( chunk ) => {
99104 data += chunk ;
100105 } ) ;
101106
102107 res . on ( 'end' , ( ) => {
103108 if ( res . statusCode === 200 ) {
104- let tokenPair = this . createTokenPairFromResponse ( data ) ;
109+ let tokenPair = this . createTokenPairFromResponse ( data , oldToken ) ;
105110 resolve ( tokenPair ) ;
106111 } else {
107112 reject ( new Error ( data ) ) ;
@@ -124,6 +129,7 @@ export class SpotifyAPI {
124129 method : "POST" ,
125130 headers : {
126131 "Accept" : "Application/json" ,
132+ "Content-Type" : "application/x-www-form-urlencoded"
127133 }
128134 }
129135 }
@@ -137,6 +143,7 @@ export class SpotifyAPI {
137143 method : "POST" ,
138144 headers : {
139145 "Accept" : "Application/json" ,
146+ "Content-Type" : "application/x-www-form-urlencoded"
140147 }
141148 }
142149 }
@@ -148,20 +155,22 @@ export class SpotifyAPI {
148155 path : `/v1/me/player/queue?uri=${ trackURI } ` ,
149156 method : "POST" ,
150157 headers : {
151- "Authorization" : `Bearer ${ access_token } ` ,
158+ "Authorization" : `Bearer ${ access_token } `
152159 }
153160 }
154161 }
155162
156- private createTokenPairFromResponse ( response : string ) : TokenPair {
163+ private createTokenPairFromResponse ( response : string , oldTokenPair ? : TokenPair ) : TokenPair {
157164 let responseObject = JSON . parse ( response ) ;
158165
159- if ( ! responseObject . access_token || ! responseObject . token_type || ! responseObject . scope || ! responseObject . expires_in || ! responseObject . refresh_token )
166+ if ( ! responseObject . access_token || ! responseObject . token_type || ! responseObject . scope || ! responseObject . expires_in )
160167 throw new Error ( "Not all information received" ) ;
161168
162169 let now = new Date ( ) ;
163170 let expiration = new Date ( ( new Date ( ) ) . setSeconds ( now . getSeconds ( ) + parseInt ( responseObject . expires_in ) ) ) ;
164171
165- return new TokenPair ( responseObject . access_token , responseObject . refresh_token , expiration ) ;
172+ let refreshToken = responseObject . refresh_token ? responseObject . refresh_token : oldTokenPair ?. refreshToken ;
173+
174+ return new TokenPair ( responseObject . access_token , refreshToken , expiration ) ;
166175 }
167176}
0 commit comments