Search in sources :

Example 1 with AuthorizationCodeCredentials

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

the class CallbackController method spotifyCallback.

@SneakyThrows
@GetMapping("/spotify/callback")
public String spotifyCallback(@RequestParam(name = "code", defaultValue = "null") String code, @CookieValue(name = "session", defaultValue = "null") String session, Model model) {
    if (code.equals("null"))
        return "redirect:/erorr?code=503";
    if (sessionManagementService.getUser(session) == null)
        return "redirect:/erorr?code=503";
    BotUser user = sessionManagementService.getUser(session);
    AuthorizationCodeCredentials codeCredentials = spotifyApi.authorizationCode(code).build().execute();
    if (user.spotifyTokens() != null) {
        user.spotifyTokens().setAccessToken(codeCredentials.getAccessToken());
        user.spotifyTokens().setRefreshToken(codeCredentials.getRefreshToken());
    } else
        user.addTokenPair(new TokenPair(0L, codeCredentials.getAccessToken(), codeCredentials.getRefreshToken(), TokenType.SPOTIFY));
    repository.saveAndFlush(user);
    botInstanceManagementService.startInstance(user);
    return "redirect:/dashboard";
}
Also used : BotUser(de.notecho.spotify.database.user.entities.BotUser) TokenPair(de.notecho.spotify.database.user.entities.TokenPair) AuthorizationCodeCredentials(se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials) GetMapping(org.springframework.web.bind.annotation.GetMapping) SneakyThrows(lombok.SneakyThrows)

Example 2 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_Async.

public static void authorizationCode_Async() {
    try {
        final CompletableFuture<AuthorizationCodeCredentials> authorizationCodeCredentialsFuture = authorizationCodeRequest.executeAsync();
        // Thread free to do other tasks...
        // Example Only. Never block in production code.
        final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeCredentialsFuture.join();
        // 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 (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 3 with AuthorizationCodeCredentials

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

the class AuthorizationCodeRefreshExample method authorizationCodeRefresh_Async.

public static void authorizationCodeRefresh_Async() {
    try {
        final CompletableFuture<AuthorizationCodeCredentials> authorizationCodeCredentialsFuture = authorizationCodeRefreshRequest.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 4 with AuthorizationCodeCredentials

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

the class AuthorizationCodeRefreshExample method authorizationCodeRefresh_Sync.

public static void authorizationCodeRefresh_Sync() {
    try {
        final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeRefreshRequest.execute();
        // Set access and refresh token for further "spotifyApi" object usage
        spotifyApi.setAccessToken(authorizationCodeCredentials.getAccessToken());
        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 5 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_Async.

public static void authorizationCode_Async() {
    try {
        final CompletableFuture<AuthorizationCodeCredentials> authorizationCodeCredentialsFuture = authorizationCodePKCERequest.executeAsync();
        // Thread free to do other tasks...
        // Example Only. Never block in production code.
        final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeCredentialsFuture.join();
        // 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 (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)

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