forked from Fascinated/Bat
spotify debug
This commit is contained in:
parent
53b84a884c
commit
eccd673db8
@ -19,6 +19,11 @@ public class SpotifyProfile extends Profile {
|
||||
*/
|
||||
private String refreshToken;
|
||||
|
||||
/**
|
||||
* When the access token expires
|
||||
*/
|
||||
private Long expiresAt;
|
||||
|
||||
public SpotifyProfile() {
|
||||
super("spotify");
|
||||
}
|
||||
|
@ -91,6 +91,7 @@ public class SpotifyService {
|
||||
try {
|
||||
return getSpotifyApi(user).getUsersCurrentlyPlayingTrack().build().execute();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -178,11 +179,26 @@ public class SpotifyService {
|
||||
*
|
||||
* @return the Spotify API
|
||||
*/
|
||||
@SneakyThrows
|
||||
public SpotifyApi getSpotifyApi(BatUser user) {
|
||||
SpotifyProfile profile = user.getProfile(SpotifyProfile.class);
|
||||
return new SpotifyApi.Builder()
|
||||
.setAccessToken(profile.getAccessToken())
|
||||
SpotifyApi api = new SpotifyApi.Builder()
|
||||
.setClientId(clientId)
|
||||
.setClientSecret(clientSecret)
|
||||
.setAccessToken(profile.getAccessToken())
|
||||
.setRefreshToken(profile.getRefreshToken())
|
||||
.build();
|
||||
|
||||
// Refresh the access token if it's expired
|
||||
if (profile.getExpiresAt() == null || profile.getExpiresAt() < System.currentTimeMillis()) {
|
||||
AuthorizationCodeCredentials credentials = api.authorizationCodeRefresh().build().execute();
|
||||
profile.setAccessToken(credentials.getAccessToken());
|
||||
profile.setRefreshToken(credentials.getRefreshToken());
|
||||
profile.setExpiresAt(System.currentTimeMillis() + (credentials.getExpiresIn() * 1000));
|
||||
api.setAccessToken(credentials.getAccessToken());
|
||||
api.setRefreshToken(credentials.getRefreshToken());
|
||||
userService.saveUser(user);
|
||||
}
|
||||
return api;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user