@@ -15,13 +15,15 @@ import com.adamratzman.spotify.endpoints.public.PlaylistsAPI
1515import com.adamratzman.spotify.endpoints.public.SearchAPI
1616import com.adamratzman.spotify.endpoints.public.TracksAPI
1717import com.adamratzman.spotify.endpoints.public.UserAPI
18+ import com.adamratzman.spotify.utils.HttpConnection
19+ import com.adamratzman.spotify.utils.HttpHeader
20+ import com.adamratzman.spotify.utils.HttpRequestMethod
1821import com.adamratzman.spotify.utils.SpotifyEndpoint
1922import com.adamratzman.spotify.utils.Token
2023import com.adamratzman.spotify.utils.byteEncode
2124import com.adamratzman.spotify.utils.toObject
2225import com.google.gson.Gson
2326import com.google.gson.GsonBuilder
24- import org.jsoup.Jsoup
2527import java.util.concurrent.Executors
2628import java.util.concurrent.TimeUnit
2729
@@ -159,17 +161,10 @@ class SpotifyApiBuilder {
159161 )
160162 }
161163 else -> try {
162- val token = Gson ().fromJson(
163- Jsoup .connect(" https://accounts.spotify.com/api/token" )
164- .data(" grant_type" , " client_credentials" )
165- .header(" Authorization" , " Basic " + (" $clientId :$clientSecret " .byteEncode()))
166- .ignoreContentType(true ).post().body().text(), Token ::class .java
167- ) ? : throw IllegalArgumentException (" Invalid credentials provided" )
168- SpotifyAppAPI (
169- clientId ? : throw IllegalArgumentException (),
170- clientSecret ? : throw IllegalArgumentException (),
171- token
172- )
164+ if (clientId == null || clientSecret == null ) throw IllegalArgumentException (" Illegal credentials provided" )
165+ val token = getCredentialedToken(clientId, clientSecret)
166+ ? : throw IllegalArgumentException (" Invalid credentials provided" )
167+ SpotifyAppAPI (clientId, clientSecret, token)
173168 } catch (e: Exception ) {
174169 throw SpotifyException (" Invalid credentials provided in the login process" , e)
175170 }
@@ -210,12 +205,12 @@ class SpotifyApiBuilder {
210205 SpotifyClientAPI (
211206 clientId ? : throw IllegalArgumentException (),
212207 clientSecret ? : throw IllegalArgumentException (),
213- Jsoup .connect( " https://accounts.spotify.com/api/token " )
214- .data( " grant_type " , " authorization_code " )
215- .data( " code " , authorizationCode)
216- .data( " redirect_uri" , redirectUri)
217- .header( " Authorization " , " Basic " + ( " $clientId : $clientSecret " ).byteEncode())
218- .ignoreContentType( true ).post().body().text() .toObject(Gson (), Token ::class .java),
208+ HttpConnection (
209+ url = " https://accounts.spotify.com/api/token " ,
210+ method = HttpRequestMethod . POST ,
211+ body = " grant_type=authorization_code&code= $authorizationCode & redirect_uri= $redirectUri " ,
212+ contentType = " application/x-www-form-urlencoded "
213+ ).execute( HttpHeader ( " Authorization " , " Basic ${ " $clientId : $clientSecret " .byteEncode()} " )).body .toObject(Gson (), Token ::class .java),
219214 automaticRefresh,
220215 redirectUri ? : throw IllegalArgumentException ()
221216 )
@@ -237,7 +232,7 @@ class SpotifyApiBuilder {
237232 )
238233 else -> throw IllegalArgumentException (
239234 " At least one of: authorizationCode, tokenString, or token must be provided " +
240- " to build a SpotifyClientAPI object"
235+ " to build a SpotifyClientAPI object"
241236 )
242237 }
243238 }
@@ -300,12 +295,7 @@ class SpotifyAppAPI internal constructor(clientId: String, clientSecret: String,
300295
301296 override fun refreshToken () {
302297 if (clientId != " not-set" && clientSecret != " not-set" )
303- token = gson.fromJson(
304- Jsoup .connect(" https://accounts.spotify.com/api/token" )
305- .data(" grant_type" , " client_credentials" )
306- .header(" Authorization" , " Basic " + (" $clientId :$clientSecret " .byteEncode()))
307- .ignoreContentType(true ).post().body().text(), Token ::class .java
308- )
298+ token = getCredentialedToken(clientId, clientSecret)
309299 expireTime = System .currentTimeMillis() + token.expires_in * 1000
310300 }
311301
@@ -372,11 +362,13 @@ class SpotifyClientAPI internal constructor(
372362
373363 override fun refreshToken () {
374364 val tempToken = gson.fromJson(
375- Jsoup .connect(" https://accounts.spotify.com/api/token" )
376- .data(" grant_type" , " refresh_token" )
377- .data(" refresh_token" , token.refresh_token ? : " " )
378- .header(" Authorization" , " Basic " + (" $clientId :$clientSecret " ).byteEncode())
379- .ignoreContentType(true ).post().body().text(), Token ::class .java
365+ HttpConnection (
366+ url = " https://accounts.spotify.com/api/token" ,
367+ method = HttpRequestMethod .POST ,
368+ body = " grant_type=refresh_token&refresh_token=${token.refresh_token ? : " " } " ,
369+ contentType = " application/x-www-form-urlencoded"
370+ ).execute(HttpHeader (" Authorization" , " Basic ${" $clientId :$clientSecret " .byteEncode()} " )).body,
371+ Token ::class .java
380372 )
381373 if (tempToken == null ) {
382374 logger.logWarning(" Spotify token refresh failed" )
@@ -407,7 +399,17 @@ class SpotifyClientAPI internal constructor(
407399
408400private fun getAuthUrlFull (vararg scopes : SpotifyScope , clientId : String , redirectUri : String ): String {
409401 return " https://accounts.spotify.com/authorize/?client_id=$clientId " +
410- " &response_type=code" +
411- " &redirect_uri=$redirectUri " +
412- if (scopes.isEmpty()) " " else " &scope=${scopes.joinToString(" %20" ) { it.uri }} "
402+ " &response_type=code" +
403+ " &redirect_uri=$redirectUri " +
404+ if (scopes.isEmpty()) " " else " &scope=${scopes.joinToString(" %20" ) { it.uri }} "
413405}
406+
407+ private fun getCredentialedToken (clientId : String , clientSecret : String ) = Gson ().fromJson(
408+ HttpConnection (
409+ url = " https://accounts.spotify.com/api/token" ,
410+ method = HttpRequestMethod .POST ,
411+ body = " grant_type=client_credentials" ,
412+ contentType = " application/x-www-form-urlencoded"
413+ ).execute(HttpHeader (" Authorization" , " Basic ${" $clientId :$clientSecret " .byteEncode()} " )).body,
414+ Token ::class .java
415+ )
0 commit comments