diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java index a1266ee..8a34aec 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/LastFMToSpotify.java @@ -83,14 +83,12 @@ public class LastFMToSpotify { if (configuration.containsKey("cache.crypto") && TokenHelper.existsTokens()) { logLn("Cached credentials have been found.", 2); logLn("Fetching credentials from cache.", 2); - SpotifyCredentials oldcred = TokenHelper.fetchTokens(); - api.setRefreshToken(oldcred.getRefreshToken()); - SpotifyCredentials cred; - if(oldcred.isValid()){ - cred=oldcred; - } else { + SpotifyCredentials cred = TokenHelper.fetchTokens(); + api.setRefreshToken(cred.getRefreshToken()); + + if(!cred.isValid()){ logLn("Cached credentials are invalid due to age. Refreshing and saving to cache", 2); - cred = new SpotifyCredentials(api.authorizationCodeRefresh().build().execute()); + cred.refreshCredentials(api.authorizationCodeRefresh().build().execute()); TokenHelper.saveTokens(cred); } configuration.put("spotify.access", cred.getAccessToken()); diff --git a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/SpotifyCredentials.java b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/SpotifyCredentials.java index 3b0148c..5c5f4d2 100644 --- a/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/SpotifyCredentials.java +++ b/src/main/java/de/b00tload/tools/lastfmtospotifyplaylist/util/SpotifyCredentials.java @@ -4,21 +4,24 @@ import se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCrede import java.io.Serializable; import java.time.Clock; import java.time.LocalDateTime; +import java.util.Objects; /** * A wrapper class for se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials. Implements checking validity of access token. */ public class SpotifyCredentials implements Serializable { - private final AuthorizationCodeCredentials cred; - private final LocalDateTime validUntil; + private String accessToken; + private String refreshToken; + private LocalDateTime validUntil; /** * Initializes the class * @param cred The AuthorizationCodeCredentials to be saved. Recommended for use with recently (last few seconds) generated Credentials */ public SpotifyCredentials(AuthorizationCodeCredentials cred){ - this.cred = cred; + this.accessToken = cred.getAccessToken(); + this.refreshToken = cred.getRefreshToken(); this.validUntil = LocalDateTime.now(Clock.systemDefaultZone()).plusSeconds(cred.getExpiresIn()); } @@ -27,7 +30,7 @@ public class SpotifyCredentials implements Serializable { * @return An access token that can be provided in subsequent calls, for example to Spotify Web API services. */ public String getAccessToken(){ - return cred.getAccessToken(); + return accessToken; } /** @@ -35,7 +38,7 @@ public class SpotifyCredentials implements Serializable { * @return A token that can be sent to the Spotify Accounts service in place of an access token. */ public String getRefreshToken(){ - return cred.getRefreshToken(); + return refreshToken; } /** @@ -54,4 +57,14 @@ public class SpotifyCredentials implements Serializable { return LocalDateTime.now(Clock.systemDefaultZone()).isBefore(getValidUntil()); } + /** + * Refreshes the access token. If a new refresh token is provided it will be saved as well. + * @param cred The AuthorizationCodeCredentials to be saved. Recommended for use with recently (last few seconds) generated Credentials + */ + public void refreshCredentials(AuthorizationCodeCredentials cred){ + this.accessToken = cred.getAccessToken(); + if(Objects.nonNull(cred.getRefreshToken())) this.refreshToken = cred.getRefreshToken(); + this.validUntil = LocalDateTime.now(Clock.systemDefaultZone()).plusSeconds(cred.getExpiresIn()); + } + } \ No newline at end of file