Search in sources :

Example 6 with AuthorizationCodeCredentials

use of se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials in project spotify-web-api-java by spotify-web-api-java.

the class AuthorizationCodePKCEExample method authorizationCode_Sync.

public static void authorizationCode_Sync() {
    try {
        final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodePKCERequest.execute();
        // Set access and refresh token for further "spotifyApi" object usage
        spotifyApi.setAccessToken(authorizationCodeCredentials.getAccessToken());
        spotifyApi.setRefreshToken(authorizationCodeCredentials.getRefreshToken());
        System.out.println("Expires in: " + authorizationCodeCredentials.getExpiresIn());
    } catch (IOException | SpotifyWebApiException | ParseException e) {
        System.out.println("Error: " + e.getMessage());
    }
}
Also used : IOException(java.io.IOException) ParseException(org.apache.hc.core5.http.ParseException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) AuthorizationCodeCredentials(se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials)

Example 7 with AuthorizationCodeCredentials

use of se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials in project spotify-web-api-java by spotify-web-api-java.

the class AuthorizationCodeExample method authorizationCode_Sync.

public static void authorizationCode_Sync() {
    try {
        final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeRequest.execute();
        // Set access and refresh token for further "spotifyApi" object usage
        spotifyApi.setAccessToken(authorizationCodeCredentials.getAccessToken());
        spotifyApi.setRefreshToken(authorizationCodeCredentials.getRefreshToken());
        System.out.println("Expires in: " + authorizationCodeCredentials.getExpiresIn());
    } catch (IOException | SpotifyWebApiException | ParseException e) {
        System.out.println("Error: " + e.getMessage());
    }
}
Also used : IOException(java.io.IOException) ParseException(org.apache.hc.core5.http.ParseException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) AuthorizationCodeCredentials(se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials)

Example 8 with AuthorizationCodeCredentials

use of se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials in project spotify-web-api-java by spotify-web-api-java.

the class AuthorizationCodePKCERefreshExample method authorizationCodeRefresh_Sync.

public static void authorizationCodeRefresh_Sync() {
    try {
        final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodePKCERefreshRequest.execute();
        // Set access and refresh token for further "spotifyApi" object usage
        spotifyApi.setAccessToken(authorizationCodeCredentials.getAccessToken());
        spotifyApi.setRefreshToken(authorizationCodeCredentials.getRefreshToken());
        System.out.println("Expires in: " + authorizationCodeCredentials.getExpiresIn());
    } catch (IOException | SpotifyWebApiException | ParseException e) {
        System.out.println("Error: " + e.getMessage());
    }
}
Also used : IOException(java.io.IOException) ParseException(org.apache.hc.core5.http.ParseException) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) AuthorizationCodeCredentials(se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials)

Example 9 with AuthorizationCodeCredentials

use of se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials in project spotify-web-api-java by spotify-web-api-java.

the class AuthorizationCodePKCERefreshExample method authorizationCodeRefresh_Async.

public static void authorizationCodeRefresh_Async() {
    try {
        final CompletableFuture<AuthorizationCodeCredentials> authorizationCodeCredentialsFuture = authorizationCodePKCERefreshRequest.executeAsync();
        // Thread free to do other tasks...
        // Example Only. Never block in production code.
        final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeCredentialsFuture.join();
        // Set access token for further "spotifyApi" object usage
        spotifyApi.setAccessToken(authorizationCodeCredentials.getAccessToken());
        System.out.println("Expires in: " + authorizationCodeCredentials.getExpiresIn());
    } catch (CompletionException e) {
        System.out.println("Error: " + e.getCause().getMessage());
    } catch (CancellationException e) {
        System.out.println("Async operation cancelled.");
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) CompletionException(java.util.concurrent.CompletionException) AuthorizationCodeCredentials(se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials)

Example 10 with AuthorizationCodeCredentials

use of se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials in project spotifybot by NotEchoDE.

the class BotInstance method updateTokens.

private void updateTokens() {
    if (user.spotifyTokens() == null) {
        context.getBean(BotInstanceManagementService.class).stopInstance(user);
        return;
    }
    updateTwitchToken(user.twitchTokens());
    if (user.chatAccountTokens() != null)
        updateTwitchToken(user.chatAccountTokens());
    try {
        this.spotifyApi.setRefreshToken(user.spotifyTokens().getRefreshToken());
        AuthorizationCodeRefreshRequest authorizationCodeRefreshRequest = spotifyApi.authorizationCodeRefresh().build();
        final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeRefreshRequest.execute();
        spotifyApi.setAccessToken(authorizationCodeCredentials.getAccessToken());
        spotifyApi.setRefreshToken(authorizationCodeCredentials.getRefreshToken());
        user.spotifyTokens().setAccessToken(authorizationCodeCredentials.getAccessToken());
        Logger.log(LogType.DEBUG, "[" + user.getId() + "] Got new Spotify Token for " + login + ", expires in: " + authorizationCodeCredentials.getExpiresIn() + " | " + authorizationCodeCredentials.getAccessToken().substring(0, 10) + "...", "Spotify", login, String.valueOf(authorizationCodeCredentials.getExpiresIn()), authorizationCodeCredentials.getAccessToken().substring(0, 10) + "...");
    } catch (BadRequestException e) {
        TokenPair spotifyTokens = user.spotifyTokens();
        user.getTokenPairs().remove(spotifyTokens);
        context.getBean(UserRepository.class).saveAndFlush(user);
        context.getBean(TokenPairRepository.class).delete(spotifyTokens);
        Logger.log(LogType.INFO, "[" + user.getId() + "] " + login + " revoked his access token so it was removed from the database.", login, "revoked", "database");
        for (BaseModule module : this.modules) module.unregister(client);
        client.getChat().leaveChannel(this.login);
        context.getBean(BotInstanceManagementService.class).stopInstance(user);
        return;
    } catch (IOException | SpotifyWebApiException | ParseException e) {
        e.printStackTrace();
    }
    context.getBean(UserRepository.class).saveAndFlush(user);
    nextCheck = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(50);
}
Also used : UserRepository(de.notecho.spotify.database.user.repository.UserRepository) BaseModule(de.notecho.spotify.bot.modules.BaseModule) BadRequestException(se.michaelthelin.spotify.exceptions.detailed.BadRequestException) IOException(java.io.IOException) ParseException(org.apache.hc.core5.http.ParseException) AuthorizationCodeRefreshRequest(se.michaelthelin.spotify.requests.authorization.authorization_code.AuthorizationCodeRefreshRequest) TokenPair(de.notecho.spotify.database.user.entities.TokenPair) BotInstanceManagementService(de.notecho.spotify.bot.BotInstanceManagementService) SpotifyWebApiException(se.michaelthelin.spotify.exceptions.SpotifyWebApiException) AuthorizationCodeCredentials(se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials)

Aggregations

AuthorizationCodeCredentials (se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials)10 IOException (java.io.IOException)5 ParseException (org.apache.hc.core5.http.ParseException)5 SpotifyWebApiException (se.michaelthelin.spotify.exceptions.SpotifyWebApiException)5 CancellationException (java.util.concurrent.CancellationException)4 CompletionException (java.util.concurrent.CompletionException)4 TokenPair (de.notecho.spotify.database.user.entities.TokenPair)2 BotInstanceManagementService (de.notecho.spotify.bot.BotInstanceManagementService)1 BaseModule (de.notecho.spotify.bot.modules.BaseModule)1 BotUser (de.notecho.spotify.database.user.entities.BotUser)1 UserRepository (de.notecho.spotify.database.user.repository.UserRepository)1 SneakyThrows (lombok.SneakyThrows)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 BadRequestException (se.michaelthelin.spotify.exceptions.detailed.BadRequestException)1 AuthorizationCodeRefreshRequest (se.michaelthelin.spotify.requests.authorization.authorization_code.AuthorizationCodeRefreshRequest)1